laanes Posted March 7, 2011 Share Posted March 7, 2011 Hi, i've got a question in my head: How could i improve the WHERE clause in this query? : $query = "SELECT cat_name FROM " . $table . " WHERE cat_id != 300 AND cat_id != 200 AND cat_id != 100"; It works, but i can imagine when there will be more excluded categories, there will be a lot of AND's. Any way to make this query more decent? Kind regards, laanes Quote Link to comment https://forums.phpfreaks.com/topic/229893-how-could-i-re-write-the-where-clause-in-this-query/ Share on other sites More sharing options...
Maq Posted March 7, 2011 Share Posted March 7, 2011 You could use NOT IN. Quote Link to comment https://forums.phpfreaks.com/topic/229893-how-could-i-re-write-the-where-clause-in-this-query/#findComment-1184090 Share on other sites More sharing options...
fenway Posted March 7, 2011 Share Posted March 7, 2011 But that's the most inefficient query you could possibly write. Quote Link to comment https://forums.phpfreaks.com/topic/229893-how-could-i-re-write-the-where-clause-in-this-query/#findComment-1184214 Share on other sites More sharing options...
laanes Posted March 8, 2011 Author Share Posted March 8, 2011 You could use NOT IN. So.. something like $query = "SELECT * FROM " .$table. " WHERE 100, 200, 300 NOT IN " .$table. ".cat_id"; ? But that's the most inefficient query you could possibly write. What would be a better way then? Quote Link to comment https://forums.phpfreaks.com/topic/229893-how-could-i-re-write-the-where-clause-in-this-query/#findComment-1184431 Share on other sites More sharing options...
kickstart Posted March 8, 2011 Share Posted March 8, 2011 Hi Not quite. $query = "SELECT * FROM " .$table. " WHERE cat_id NOT IN (100, 200, 300)"; All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/229893-how-could-i-re-write-the-where-clause-in-this-query/#findComment-1184440 Share on other sites More sharing options...
laanes Posted March 10, 2011 Author Share Posted March 10, 2011 This works, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/229893-how-could-i-re-write-the-where-clause-in-this-query/#findComment-1185483 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.