Forum Moderators: coopster

Message Too Old, No Replies

Too many results from query

         

yowza

10:28 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



I don't think there is a SQL forum on WW, so I posted here.

I'm trying to return all rows from one table1 that don't have a matching record in table2. This was very easy to do.

The problem is that if all records from table1 have a match in table 2, all rows are returned when no results should be returned.

I've tried forever to figure this out, but have had no luck.

Here's the SQL:

SELECT books.title,books.category,books.prodid FROM books,categories WHERE books.category!= categories.catid

If there is one record in books.category that is not in categories.catid, it returns the results that I want. However, if they both have matching records, it returns all results.

Thanks.

ergophobe

10:33 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



see message #7 this thread:

[webmasterworld.com...]

Tom

yowza

11:13 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



I didn't really see anything in that thread.

I've already tried an inner join with the same result as above.

coopster

11:19 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think ergophobe meant for you to use a
LEFT JOIN
.

If there is no matching record for the right table in the

ON
or
USING
part in a LEFT JOIN [mysql.com], a row with all columns set to
NULL
is used for the right table. You can use this fact to find records in a table that have no counterpart in another table:
SELECT 
books.title,
books.category,
books.prodid
FROM books
LEFT JOIN categories
ON books.category = categories.catid
WHERE categories.catid IS NULL
;

yowza

12:51 am on Mar 12, 2004 (gmt 0)

10+ Year Member



Beautiful!

I never took "categories" out of FROM. That was my only problem when I tried the JOIN before.

Thanks!