If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SELECT query..

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-01-04, 23:52
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
SELECT query..

Hi,

I was wondering if the following can be improved using NATURAL JOIN:

SELECT *
FROM logins, profiles, avatars, prefs, groups, tmpl
WHERE username='john'
AND logins.some_id = profiles.some_id
AND profiles.some_id=prefs.some_id
AND profiles.avatar_id=avatars.avatar_id
AND profiles.group_id=groups.group_id
AND prefs.tmpl_id=tmpl.tmpl_id

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-02-04, 09:03
roga roga is offline
Registered User
 
Join Date: Jan 2004
Location: Germany
Posts: 17
Assuming that 'some_id' exists in profiles and prefs, try this:
Code:
SELECT *
FROM logins
LEFT JOIN profiles USING (some_id)
LEFT JOIN prefs USING (some_id)
LEFT JOIN avatars ON profiles.avatar_id=avatars.avatar_id
LEFT JOIN groups ON profiles.group_id=groups.group_id
LEFT JOIN tmpl ON prefs.tmpl_id=tmpl.tmpl_id
WHERE username='john'
I'm not sure if this works too:
Code:
SELECT *
FROM logins
LEFT JOIN profiles USING (some_id)
LEFT JOIN prefs USING (some_id)
LEFT JOIN avatars USING (avatar_id)
LEFT JOIN groups USING(group_id)
LEFT JOIN tmpl USING(tmpl_id)
WHERE username='john'
Reply With Quote
  #3 (permalink)  
Old 01-03-04, 22:58
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Great thanks, roga!
Code:
SELECT *
FROM logins
LEFT JOIN profiles USING (some_id)
LEFT JOIN prefs USING (some_id)
LEFT JOIN avatars ON profiles.avatar_id=avatars.avatar_id
LEFT JOIN groups ON profiles.group_id=groups.group_id
LEFT JOIN tmpl ON prefs.tmpl_id=tmpl.tmpl_id
WHERE username='john'
The version about works but the other one doesn't.

I'm curious if there're any performance differences between the one you gave above and the one I've originally using AND, reproduced below:
Code:
SELECT *
FROM logins, profiles, avatars, prefs, groups, tmpl
WHERE username='john'
AND logins.some_id = profiles.some_id
AND profiles.some_id=prefs.some_id
AND profiles.avatar_id=avatars.avatar_id
AND profiles.group_id=groups.group_id
AND prefs.tmpl_id=tmpl.tmpl_id
Thanks in anticipation
Reply With Quote
  #4 (permalink)  
Old 01-04-04, 02:34
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
I'm curious if there're any performance differences between the one you gave above and the one I've originally using AND
performance differences? probably

but more significant is the difference in results

your original query used inner joins, whereas roga's used left joins!!

the results would be the same only if there happened to be no unmatched rows

also, while the mysql manual is not very clear about this, a NATURAL join in standard sql does not have a USING clause, rather, it joins tables based on all identically named columns

so the original query re-written with NATURAL joins would be

SELECT *
FROM logins
NATURAL JOIN profiles
NATURAL JOIN avatars
NATURAL JOIN prefs
NATURAL JOIN groups
NATURAL JOIN tmpl
WHERE username='john'

if you use the convention of naming an auto_increment column as "id" and a relationship column as "some_id" then both tables would have an "id" column and the NATURAL join would try to join based on the that, which would of course be incorrect
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-04-04, 02:45
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Thanks, r937!
Quote:
the results would be the same only if there happened to be no unmatched rows
So I should be using LEFT JOIN instead of AND?

Can you give an example or perhaps elaborate on when LEFT JOIN should be used?
Reply With Quote
  #6 (permalink)  
Old 01-04-04, 02:53
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
a LEFT join should be used whenever you want all rows from the left table, together with matching rows from the right table, whereas with an INNER join, you get only matching rows

consider

Pets
1 dog
2 cat
3 bird
4 ferret

People
35 curly
38 larry
39 moe

PeoplePets
35 2
35 3
39 1


list all people, and their pets if any (LEFT join) --

curly cat
curly bird
larry NULL
moe dog

list all people with pets (INNER join) --

curly cat
curly bird
moe dog
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 01-04-04, 04:53
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Thanks for your example! I think I roughly get the idea

cheers
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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