TEENFRONT Posted November 30, 2008 Share Posted November 30, 2008 i need to select from database where id= 1, 2, 3, 4, 5. How do i do that? all the code snippets i find online and tuts just do WHERE id=1 but i need to specifically select a few ids. if i use AND, it returns nothing? im sure its uber simple but i cannot find anything for the life of me..i tried searching around but i end up with the simple where clause and not multiple where lol. hoping you can help Link to comment https://forums.phpfreaks.com/topic/134860-silly-question-where-clause/ Share on other sites More sharing options...
josephman1988 Posted November 30, 2008 Share Posted November 30, 2008 Couldn't you do WHERE id <=5 .. I have to say though I'm not an expert in SQL. Link to comment https://forums.phpfreaks.com/topic/134860-silly-question-where-clause/#findComment-702210 Share on other sites More sharing options...
TEENFRONT Posted November 30, 2008 Author Share Posted November 30, 2008 no no, the id can range from 1 up to 700 in this case.. sorry for my bad example.. i need to do, select * from database WHERE id=1 and id=7 and id=78 and id=150 etc etc...theres 5 specific rows i need to select.. Link to comment https://forums.phpfreaks.com/topic/134860-silly-question-where-clause/#findComment-702214 Share on other sites More sharing options...
laPistola Posted November 30, 2008 Share Posted November 30, 2008 WHERE `id` IN (1,2,3,4,5) Link to comment https://forums.phpfreaks.com/topic/134860-silly-question-where-clause/#findComment-702225 Share on other sites More sharing options...
Flames Posted November 30, 2008 Share Posted November 30, 2008 mysql related topic. Link to comment https://forums.phpfreaks.com/topic/134860-silly-question-where-clause/#findComment-702229 Share on other sites More sharing options...
PFMaBiSmAd Posted November 30, 2008 Share Posted November 30, 2008 The reason why AND does not work is that a column can only have a single value at any time. Writing: WHERE id=1 and id=7 ... is asking for rows WHERE id=1 and id=7... at that same time and that cannot exist. You would actually need to use OR, which is logically the same as the IN() function that laPistola posted. Link to comment https://forums.phpfreaks.com/topic/134860-silly-question-where-clause/#findComment-702268 Share on other sites More sharing options...
TEENFRONT Posted November 30, 2008 Author Share Posted November 30, 2008 Sorry for my late reply Yep OR did the trick, which is best to use OR or the IN that was posted above? any performance difference? Link to comment https://forums.phpfreaks.com/topic/134860-silly-question-where-clause/#findComment-702376 Share on other sites More sharing options...
laPistola Posted November 30, 2008 Share Posted November 30, 2008 if you already have OR programed and working then why change it, using IN will just mean there is less code. Link to comment https://forums.phpfreaks.com/topic/134860-silly-question-where-clause/#findComment-702380 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.