Page 1 of 1

WHERE x = y works, but WHERE x != y doesn't

Posted: Sat Feb 12, 2011 10:19 am
by rhecker
I have two queries, each based on two tables. The first returns all rows where a field in one table matches the other, and that query works fine, but oddly, the query which should return the opposite, all rows where there is no match, doesn't work. Any thoughts?

Code: Select all

mysql_query("select title, page_content.page_id FROM page_content, menu WHERE page_content.page_id != menu.page_id GROUP BY page_id")

Re: WHERE x = y works, but WHERE x != y doesn't

Posted: Sun Feb 13, 2011 4:14 am
by rhecker
The key to the solution turned out to be "WHERE NOT EXISTS" as follows:

Code: Select all

SELECT title, page_id FROM page_content WHERE NOT EXISTS(SELECT page_id FROM menu WHERE menu.page_id = page_content.page_id) GROUP BY page_id