简体   繁体   English

使用输入的逗号分隔字符串在逗号分隔的字符串中搜索ID

[英]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 我将得到一个可能包含1000多个逗号分隔值的输入字符串,例如

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. 我想从表中获取与输入字符串中的“ assigned_to ”或“ viewers ”匹配的所有行。 I have tried with IN() for " assigned_to ", 我尝试使用IN()作为“ 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. 这不是很有效,但是可以完成任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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