【发布时间】:2015-11-22 14:02:25
【问题描述】:
大家好,我正在做一个项目,只有当它包含数组的任何值时,我才需要选择字段
我的意思是
我有一个类似的字符串(逗号分隔)
$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);
即使值匹配,此代码也不会选择任何内容..请给我建议其他内容.... 我有一个动态字符串..
【问题讨论】:
-
简单。见规范化
-
在查询中提供示例数据——使用SQL Fiddle
-
如果您可以规范化数据库表,那么它只是一个使用
IN测试的简单查询?也许有趣? Can I resolve this with pure mysql? (joining on ; separated values)