php - 从逗号分隔值 mysql 和数组中查找并选择

标签 php mysql arrays mysqli

大家好,我正在开发一个项目,仅当该字段包含数组的任何值时,我才需要选择该字段

我的意思是

我有一个类似的字符串(逗号分隔)

 $string =1,2,3,4,5,7;

我有一个数据库,其中包含一个表的重量值,例如

 1,9 // this is one of the field which conatins the id of people who should be notified

现在我想选择包含字符串中任何值的所有字段,例如

如果字符串类似于

  $string =1,7;

 field contains 1,8
 filed2 contains 9,88
 field3 contains 2,5,6,7,8

然后仅选择 1 和 3 个字段

我已经尝试过了

      select aid,a.cid,tdid,fdid,mcat,sub,a.mess,attach,prio,sdate,edate,stime,etime,a.date,d.name,d.photo,c.curl,c.cname 
 from activity a,dept d,enter c where 
 ((CONCAT(",", tdid, ",") REGEXP ",('.implode("|",$arr).'),") and tdid=d.did and d.acid=c.cid)
 $mydeptslist=1,8 
 $arr=array($mydeptslist);

即使值匹配,此代码也不会选择任何内容..请建议我其他内容.... 我有一个动态字符串..

最佳答案

MySQL函数find_in_set()只能搜索一组字符串中的一个字符串。您可以尝试这样的操作

find_in_set('1',comma_sep_field_name) OR find_in_set('7', comma_sep_field_name) 

关于php - 从逗号分隔值 mysql 和数组中查找并选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33855619/

相关文章:

php - Bootstrap 模式中的表单不会将 PHP 值发布到另一个页面

MySQL 运行总计

php - 在从 mysql 驱动的选择选项中选择选定的文本

mysql - 在字符串中从末尾开始计数的特定索引处插入子字符串。如何在mysql中?

php - 生成具有不规则步长的范围。优化

arrays - 字符串数组中的回文

javascript - 使用 Ajax 将带有字母数字键的数组发送到 PHP 脚本

PHP 查询正确但结果不匹配?

php - 仅当 Woocommerce 中的产品类别中有描述时才显示 h2 标签

php - 在 php 的 javascript 中获取 Checkbox[] 值