EchoFool Posted December 2, 2011 Share Posted December 2, 2011 Hey I am wondering how i can check in_array values in SQL with two different types of checks. I want to look for rows which have "all" the values... OR rows which have at least one of the values... here is an example of my array: Currently i use: <php //fieldname in this instance = 1,2 //Syntax in my query FieldName In ('1,2,3,4') ?> But im not sure if that means its checking if its at least one of the 4 numbers... or if its checking if FieldName contains all 4 numbers...? Does any one know the syntax for both circumstances? Quote Link to comment Share on other sites More sharing options...
trq Posted December 2, 2011 Share Posted December 2, 2011 An IN statement check to see if the field contains any of those numbers. As for your second circumstance, you shouldn't be storing multiple values in a single field in the first place. Quote Link to comment Share on other sites More sharing options...
kickstart Posted December 2, 2011 Share Posted December 2, 2011 Hi In the case you have there it is going to look for '1,2', and not find it as it the only option is the string '1,2,3,4'. You would need to explode Fieldname into an array, and check for each member of the array being withing (1,2,3,4) without the quotes around them. Something like:- $sql = 'SELECT * FROM SomeTable WHERE '.explode(' IN (1,2,3,4) OR ',FieldName).' IN (1,2,3,4) '; (note the above will screw up if FieldName is empty). All the best Keith 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.