lordzardeck Posted May 10, 2009 Share Posted May 10, 2009 My goal in this query is to search for books and return ones with certain ids and words in a column. For example, I have a table of books with title and itemid columns. And i have 4 itemid values: 5, 67, 46, 12. I also want to only return the values of those books whose title contain "exploring". So if the table was this: 5 exploring world 67 hello world 46 exploring life 12 exploring earth 345 this is random 23 exploring this 1 bar I should return rows 1, 3, and 4. What can I do to get this? I tried: SELECT * FROM thetable WHERE itemid = 5 OR itemid = 67 OR itemid = 46 OR itemid =12 OR title LIKE '%exploring%' But that would bring up rows 1, 2, 3, 4, and 6. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/157616-solved-complicated-query/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 10, 2009 Share Posted May 10, 2009 WHERE itemid IN(5,67,46,12) AND title LIKE '%exploring%' The IN() function operates like multiple OR (saves some typing.) Then you need to use AND if you want both conditions to be true. OR would mean either condition is true. Quote Link to comment https://forums.phpfreaks.com/topic/157616-solved-complicated-query/#findComment-831109 Share on other sites More sharing options...
lordzardeck Posted May 10, 2009 Author Share Posted May 10, 2009 Thanks! That fixed it. I had never heard of IN() before, so this is a great tool that i learned! Quote Link to comment https://forums.phpfreaks.com/topic/157616-solved-complicated-query/#findComment-831123 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.