[英]Count the number of rows in SQL using a string as criteria
我的字符串$ niveis具有以下值(示例):
3,8,10
使用此字符串,我需要计算表内容中的行数,其中访问匹配这三个值中的任何一个。
例如,如果表内容为:
id access
1 2
2 3
3 3
4 7
5 8
6 9
7 10
8 10
应该返回5。
我在PHP中尝试了以下查询:
$query="SELECT count(id) FROM #__content WHERE access =$niveis";
但这不起作用,有人可以帮忙吗?
您可以使用IN
关键字
SELECT COUNT(id)
FROM #__content
WHERE access IN ($niveis)
您可以使用find_in_set()
:
select count(id)
from #__content
where find_in_set(access, $niveis) > 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.