Jump to content

MYSQL in_array?


EchoFool

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.