The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > Database


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-18-2007, 11:35 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon9 mySQL Subqueries

Can anybody help me out with subqueries? I don't really understand on how it works... There are only 2 things that we can connect from the current query to the sub queries right? Here's my queries, and gave me an error:

Code:
select TrackTitle, LengthSeconds from Tracks where
  LengthSeconds > all(select LengthSeconds from Tracks where
    TrackTitle = any(select Titles t2, Tracks t3 where
      t2.TitleID = t3.TitleID and t2.Genre = 'metal')))

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 07-19-2007, 04:12 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
I dont get what your asking, could you maybe reword it a little?
Reply With Quote
  #3 (permalink)  
Old 07-19-2007, 11:21 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 416
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
Would you mind:

1) Giving the database structure (via a data dictionary, for example).
2) Indicate which version of SQL you're running, including version. MySQL, for example, has widely different capabilities between 4.x and 5.x.
3) State the exact question that your query ought to answer.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-19-2007, 11:34 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
I'm not familiar with what your doing there (Don't know the keywords all or any) but how sub-queries work is they build a temporary table that your main table can join to.

So if you did:
Select UserID, UserName from User Where StatusID = 1

You could Join with SubQuery to Parent Query on UserID:
Select WebSiteName, UserID from WebSite
Left Outer Join (Select UserID, UserName from User Where StatusID = 1) UserTableJoiner on UserTableJoiner.UserID = WebSite.UserID

This would join Website to User where User's Status = 1

You just have to think from the Inner to the Outer. Think of what you need to join to your parent query. The inner query will join to the outer query with the UserID - so our UserID has to be in the inner and outer queries and they will match. Then you could put that query in another sub-query and join with a master query:

Select CompanyName, WebsiteID From Company Left Outer Join
(Select WebsiteID, WebSiteName, UserID from WebSite
Left Outer Join (Select UserID, UserName from User Where StatusID = 1) UserTableJoiner on UserTableJoiner.UserID = WebSite.UserID) WebsiteTableJoiner on WebsiteTableJoiner.WebsiteID = Company.WebsiteID

You can see we added a WebsiteID as the Joining Field to join Company with Website.

And you can go on and on and on - You could wrap this with Corporation, and Wrap that with World, etc...

And a tip - try to use Sub-Queries in your From
Reply With Quote
  #5 (permalink)  
Old 07-19-2007, 11:41 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Hmm.. ccoonen, you're talking about joining tables, no, that's not what i'm trying to do. It's my fault too for being confusing, sorry haha..

I basically want to find out how to use sub-queries on my query? And then once I got that, I want to put sub-query in a subquery that is in a query. Get it...? Kind of..?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:06 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50