SELECT table1.id, table2.coupon FROM table1 LEFT JOIN table2 ON
table1.id=table2.person_id;
In your example, the WHERE is not only a join condition, but also a filter. It gives you exactly what you asked for, only those records whose id's match. A left join tells it to use all of the records in the "left" and join the other table into it. You can still add a where clause to filter the record if you like.
On Wednesday, February 19, 2003, at 04:28 PM, Dean Householder wrote:
I am perplexed trying to retrieve all the rows I need in the query I'm
doing.
I have a table with keeping track of people using ID numbers as the primary
key and another table with coupon numbers and a reference to the first
table. But not all the second table is going to have correlating matches to
the first table. So, in other words not all people have a coupon number
entered into the database.
I run the query:
SELECT table1.id, table2.coupon FROM table1, table2 WHERE
table1.id=table2.person_id;
I expect back a report of the 213 rows in table1 and their matching coupon #
if they have one. Instead, I get 174 rows and THEIR matching coupons but no
results from the others. I want to write a query that will return the id's
from table1 and the table2 coupon regardless of whether the table2.person_id
matches table1.id.
How do I do this?
Thanks,
Dean
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <mysql-unsubscribe-
[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
-- Brent Baisley Systems Architect Landover Associates, Inc. Search & Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577
--------------------------------------------------------------------- Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail <[EMAIL PROTECTED]> To unsubscribe, e-mail <[EMAIL PROTECTED]> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php