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 > double inner join?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-05-09, 14:01
stunnaman stunnaman is offline
Registered User
 
Join Date: Jan 2009
Posts: 3
Question double inner join?

Hi,

I would like to take the following sql and modify it to be able to grab two usernames from the USER table instead of one like now

right now, USERNAME gets set based on a match between USER.UID and CP.lpUIDuser...
I would also like to match USER.UID and CP.userUID and extract this username.. not quite sure how to do this though.

Code:
SELECT CP.UID, CP.parentUID, CP.userUID, CP.iniTIMESTAMP, CP.liveTIMESTAMP, CP.TYPE, CP.TITLE, CP.webTITLE, CP.DESCRIPTION, CP.viewCOUNT, CP.replyCOUNT, CP.upVOTE, CP.downVOTE, CP.customVAL1, CP.lpTIMESTAMP, CP.lpUIDuser, USER.UID, USER.USERNAME FROM CP INNER JOIN USER ON USER.UID = CP.lpUIDuser ORDER BY CP.lpTIMESTAMP DESC LIMIT 0, 15
Thanks for any help!
Reply With Quote
  #2 (permalink)  
Old 01-05-09, 14:57
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
You need to use aliases to allow you to relate to two different rows in one table within a single SQL statement.
Code:
SELECT CP.UID, CP.parentUID, CP.userUID
,  CP.iniTIMESTAMP, CP.liveTIMESTAMP, CP.TYPE
,  CP.TITLE, CP.webTITLE, CP.DESCRIPTION
,  CP.viewCOUNT, CP.replyCOUNT, CP.upVOTE
,  CP.downVOTE, CP.customVAL1, CP.lpTIMESTAMP
,  CP.lpUIDuser, A.UID, A.USERNAME
,  B.UID, B.USERNAME
   FROM CP
   INNER JOIN USER AS A
      ON A.UID = CP.lpUIDuser
   INNER JOIN USER AS B
      ON B.UID = CP.userUID
   ORDER BY CP.lpTIMESTAMP DESC 
   LIMIT 0, 15
-PatP
Reply With Quote
  #3 (permalink)  
Old 01-05-09, 15:13
stunnaman stunnaman is offline
Registered User
 
Join Date: Jan 2009
Posts: 3
Question

Thanks! code works great..but again I have the ambiguity problem when actually displaying the rows (in php)
how can I distinguish between A and B? it won't let me do $CP['A.USERNAME'], only $CP['USERNAME']
Reply With Quote
  #4 (permalink)  
Old 01-05-09, 15:15
stunnaman stunnaman is offline
Registered User
 
Join Date: Jan 2009
Posts: 3
ahhhh i got it.. just needed two more AS statements

thanks!
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