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 > Which JOIN shall I use?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-15-08, 10:26
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 117
Which JOIN shall I use?

Hello all,

I would like to JOIN two tables but there may be more matches in table 2, for example:
Code:
table 1    table 2

ID           ID   emailto

id1          id1  yes
id2          id1  no
id3          id1  no
I would like to match only one row from table 2 to table 1 that has a certain field not equal to 'yes'. How can I do this, I have this so far:
Code:
SELECT * from message INNER JOIN receiver ON message.msgID=receiver.msgID 
WHERE (message.threadID=$threadID OR message.msgID=$calledRecord OR message.msgID=$threadID) 
AND receiver.emailto=!'yes' ORDER BY message.date ASC
It seems to work but I am not so sure, anyone confirm? Am I using the right sort of JOIN?
Reply With Quote
  #2 (permalink)  
Old 12-15-08, 10:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
INNER JOIN is correct because you want at least one receiver

but matching only one receiver, that's a different problem
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-15-08, 10:56
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
SELECT *
FROM   table1
 INNER
  JOIN (
        SELECT id
        FROM   table2
        WHERE  emailto = 'yes'
       ) x
    ON table1.id = x.id
But what happens when there's more than one yes value for one id?
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 12-15-08, 11:04
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 117
Quote:
Originally Posted by georgev
But what happens when there's more than one yes value for one id?
This should neve occur unless my application makes a mistake. If it does make a mistake I will have to fix that failure, but I'm not sure how to minimize the effects of that mistake.
Reply With Quote
  #5 (permalink)  
Old 12-15-08, 11:06
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 117
Thanks for the help guys, I will test out the query and report back.
Reply With Quote
  #6 (permalink)  
Old 12-15-08, 11:08
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by compsci
This should neve occur unless my application makes a mistake. If it does make a mistake I will have to fix that failure, but I'm not sure how to minimize the effects of that mistake.
It's called a unique constraint / unique index.
Apply it across your id and emailto column
__________________
George
Twitter | Blog
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