faqts : Computers : Databases : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

30 of 45 people (67%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Select statement to join tables?

Feb 24th, 2008 00:18
dman, Carpe DM, Tim Mousel, http://www.ttnr.org


For a simple join:

SELECT a.*, b.*
FROM tblAlpha a, tblBeta b
WHERE (a.keyfield = b.foreignkey);

It's often smarter to do an outer join, especially if you need rows in 
tblAlpha to show up in your resultset even if there is no matching 
record in tblBeta:

SELECT a.*, b.*
FROM tblAlpha a LEFT OUTER JOIN tblBeta b
 ON a.keyfield = b.foreignkey;

If you want to get into the nuts and bolts of it, the MySQL doc is 
found at:
http://www.mysql.com/doc/en/JOIN.html
and ZDNet has a decent article at:
http://www.zdnet.com.au/builder/architect/database/story/0,2000034918,20
267371,00.htm