soma56 Posted April 30, 2012 Share Posted April 30, 2012 Hi, I'm new to mysql and wanted to know if something like this was possible: $query = "SELECT * FROM tableX WHERE status != 'Error' AND id != (SELECT id FROM tableY) LIMIT 1"; $get_incomplete_keyword = mysql_query($query); The idea is to only select from table 'x' where the id is NOT in table 'Y'. Is this possible? Link to comment https://forums.phpfreaks.com/topic/261852-select-all-from-x-where-id-isnt-found-in-table-y-is-this-query-possible/ Share on other sites More sharing options...
awjudd Posted April 30, 2012 Share Posted April 30, 2012 -- Slow SELECT * FROM tableX WHERE status != 'Error' AND id NOT IN (SELECT id FROM tableY) LIMIT 1 -- Faster (should work ... ish) SELECT * FROM tableX AS x LEFT JOIN tableY AS y ON x.id=y.id WHERE x.status != 'Error' AND y.id IS NULL Please Note: the NOT EQUALS and NOT IN are pretty slow options (can't use indexes). ~awjudd Link to comment https://forums.phpfreaks.com/topic/261852-select-all-from-x-where-id-isnt-found-in-table-y-is-this-query-possible/#findComment-1341742 Share on other sites More sharing options...
soma56 Posted April 30, 2012 Author Share Posted April 30, 2012 Thanks, works perfect. Link to comment https://forums.phpfreaks.com/topic/261852-select-all-from-x-where-id-isnt-found-in-table-y-is-this-query-possible/#findComment-1341749 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.