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 > Selecting rows from 2 tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-12-06, 01:40
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Selecting rows from 2 tables

Code:
SELECT *
FROM `tbl1` t1, `tbl2` t2
WHERE t1.id=56 AND t2.id = 56
I need the values from the tables tbl1, tbl2 where the id is 56.
In tbl2 there may or may not be an id=56 - in that case I would still want the query to return tbl1's row - but that wont happen because of the AND.

So how do I return the row of tbl1 when tbl2 doesnt have such an id ?
I know to get this using 2 queries - Im looking for a single query statement.

Thanks
Reply With Quote
  #2 (permalink)  
Old 02-12-06, 08:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
use a LEFT OUTER JOIN
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-12-06, 09:56
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Couldnt figure out JOINS so far - can you pl give me the example for the above ? The docs has a section on Optimization on JOINS but no example of JOINS usage.

Edit : I got this working with LEFT JOIN alone. Thanks.

Last edited by anjanesh; 02-15-06 at 01:44.
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