简体   繁体   中英

Search ids in comma separated string with an input comma separated string

Consider the table tasks

id | assigned_to | viewers
1    100           100,200,300
2    200           125,200,310
3    300           175,250,300
4    400           400,440,670
5    500           500,111,333

I will get an input string that may contain more than 1000 comma separated values, like

1,2,3,4,5,6,7,8,,,,100,,,,500,,,,1000

I want to get all the rows from the table that are matching to " assigned_to " or " viewers " from the input string. I have tried with IN() for " assigned_to ",

SELECT * FROM tasks WHERE assign_to IN ($inputString)

but then couldn't get any solution to match with " viewers " column.

Is there any way to match a comma separated string with another comma separated string?

Thanks in advance

You can try the following code:

$str = "SELECT * FROM tasks WHERE assign_to IN ($inputString) ";
$values = explode(",",$inputString);
foreach ($values as $val) {
    $str .= "AND viewers LIKE ".$val; //Replace AND with OR as per your requirement (Match all/ match any)
}

//execute query

This is not very efficient but would do the task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM