1

I have a color array and want to match the color row in MYSQL,

as long as the color row match one of the colors in array, then take it out.

the following $colorArray should match id 2 & 3, because those rows contain yellow & blue

$colorArray = array('yellow','blue');

+------+---------+------------+
| id   | product | color      |
+------+---------+------------+
|    1 | tShirt  | red,green  |
|    2 | jeans   | yellow,red |
|    3 | shorts  | black,blue |
+------+---------+------------+

I just want a MySQL SELECT syntax, I know it can be done with PHP array_intersect to do some filters, but I don't want to for some reasons.

So is it possible?

4
  • What kind of field is 'color'? Commented Jan 10, 2016 at 15:40
  • yes, it's a String (varchar) Commented Jan 10, 2016 at 16:01
  • I believe @AdelBachene's answer will work for you. Just the change $sql part from $sql = "DELETE FROM <your_table_name> WHERE REGEXP '".$colorsStr."'"; to $sql = "SELECT * FROM <your_table_name> WHERE REGEXP '".$colorsStr."'"; Commented Jan 10, 2016 at 16:13
  • have you tried my answer ? Commented Jan 10, 2016 at 16:50

6 Answers 6

1

I think you are having a problem in combining an in and a Like %S%, so A REGEXP might be more efficient, but you'd have to benchmark it to be sure, e.g.

$colorsStr = implode('|', $colorArray); // => 'yellow|blue'
$sql = "SELECT * FROM <your_table_name> WHERE REGEXP '" . $colorsStr . "'";
Sign up to request clarification or add additional context in comments.

2 Comments

OP wants to SELECT the rows, not DELETE the rows.
@Adel Bachene thanks, but i 'm not familiar with REGEXP orz
1

It is easy:

<?php
$color = array('yellow', 'red');
$query = "";
foreach($color as $c)
   $query .= "color like '%".$c."%' or ";
$sql = "SELECT * FROM <your_table_name> WHERE ".$query." 1=0";
?>

4 Comments

U will get the OR in last iteration
And OP Need this in mysql not want to use php
final query is: SELECT * FROM <your_table_name> WHERE color like '%red%' or color like '%yellow%' or 1=0 and has no problem.
@devpro Comment2: because of there are order in your data, another solutions in sql has very overhead. I suggest use PHP or change structure in sql.
0

Use FIND_IN_SET() in your query .Try this let me know. I hope I sloved your problem your required query

SELECT * FROM t3 WHERE FIND_IN_SET('yellow', colour) or FIND_IN_SET('blue', colour) 

code:

<?php
$colorArray = array('yellow','blue','red');
$where1 ="FIND_IN_SET('$colorArray[0]', colour) ";
$where="";
for($i=1;$i<sizeof($colorArray);$i++){
$color=$colorArray[$i];
$where .= "  or FIND_IN_SET('$color', colour) ";
}
$sql="SELECT * FROM t3 WHERE $where1 $where";
//echo $sql;//SELECT * FROM t3 WHERE FIND_IN_SET('yellow', colour) or FIND_IN_SET('blue', colour) or FIND_IN_SET('red', colour) 
// try running this query 
?>

Comments

0

Try this code:

$colorsStr = implode(',', $colorArray); // => 'yellow,blue'
$sql = 'SELECT id FROM <your_table_name> WHERE color IN('.$colorsStr.')';

1 Comment

Then try this: $sql = 'SELECT id FROM <your_table_name> WHERE color = \''.$colorsStr.'\'';
0

You can try something like:

$colorArray = array('yellow','blue');
$colors = join(',',$colorArray);  
$sql = "SELECT '$colors' AS colors,
       color,
       CONCAT('(', REPLACE(color, ',', '(\\,|$)|'), '(\\,|$))') AS regex,
       '$colors' REGEXP CONCAT('(', REPLACE(color, ',', '(\\,|$)|'), '(\\,|$))') AS intersect
       FROM YourTableName
       GROUP BY id HAVING intersect > 0";

2 Comments

@Taniel I asked you what kind of field is color. Can you answer please?
THANKS !! let me try later, you're very kind to help me : )
0
$colorArray = array('yellow','blue');
$colors = implode(",",$colorArray);  
$query = "SELECT color FROM TableName WHERE color = '.$colors.'";

3 Comments

Can u try this yur self?
U r initizing colors variable and using color variable
Please add some explanations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.