franzwarning Posted July 22, 2012 Share Posted July 22, 2012 Hi there, Say I have some table, foo, and I want to select all rows in foo where 'name' is a member of an array of names that I have. How would I go about doing that? Cheers, George Quote Link to comment Share on other sites More sharing options...
requinix Posted July 22, 2012 Share Posted July 22, 2012 Depends on the size of the array and where it's coming from. But in general you can WHERE field IN ("value 1", "value 2", "value 3") or WHERE (field = "value1" OR field = "value 2" OR field = "value 3") Quote Link to comment Share on other sites More sharing options...
fenway Posted July 22, 2012 Share Posted July 22, 2012 I'd definitely steer you away from multiple ORs -- just too error-prone -- and it's easy to add one more for NULLs. Quote Link to comment Share on other sites More sharing options...
Riparian Posted July 23, 2012 Share Posted July 23, 2012 One way would be to select * and in the while loop use if(in_array($row[thesurname],$the_array_of_names)){do something;} Quote Link to comment Share on other sites More sharing options...
requinix Posted July 23, 2012 Share Posted July 23, 2012 One way would be to select * and in the while loop use if(in_array($row[thesurname],$the_array_of_names)){do something;} No. Very no. Quote Link to comment Share on other sites More sharing options...
premiso Posted July 23, 2012 Share Posted July 23, 2012 One way would be to select * and in the while loop use if(in_array($row[thesurname],$the_array_of_names)){do something;} I use this method all the time. It is very efficient and does not take long at all, +1 imo. No. Very no. Stop being a negative nancy. Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 23, 2012 Share Posted July 23, 2012 Partly it depends on where the list of names is coming from. If it's another table, use a JOIN. Otherwise, use IN. Quote Link to comment Share on other sites More sharing options...
premiso Posted July 23, 2012 Share Posted July 23, 2012 Depends on the size of the array and where it's coming from. But in general you can WHERE field IN ("value 1", "value 2", "value 3") or WHERE (field = "value1" OR field = "value 2" OR field = "value 3") mm Double quotes work for string values in MySQL? Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 23, 2012 Share Posted July 23, 2012 Yes, they do. Quote Link to comment Share on other sites More sharing options...
premiso Posted July 23, 2012 Share Posted July 23, 2012 Yes, they do. apparently so....apparently so. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 25, 2012 Share Posted July 25, 2012 One way would be to select * and in the while loop use if(in_array($row[thesurname],$the_array_of_names)){do something;} That can't possibly be efficient for at least 23 different reasons. Quote Link to comment 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.