简体   繁体   English

使用字符串作为条件计算SQL中的行数

[英]Count the number of rows in SQL using a string as criteria

I have the string $niveis with the following values (example): 我的字符串$ niveis具有以下值(示例):

3,8,10

Using this string, I need to count the number of rows in table content, where the access matches any of these 3 values. 使用此字符串,我需要计算表内容中的行数,其中访问匹配这三个值中的任何一个。

For example, if table content is: 例如,如果表内容为:

id   access
1    2
2    3
3    3
4    7
5    8
6    9
7    10
8    10

Should return 5. 应该返回5。

I tried the following query in PHP: 我在PHP中尝试了以下查询:

$query="SELECT count(id) FROM #__content WHERE access =$niveis";

But it isn't working, can anyone help? 但这不起作用,有人可以帮忙吗?

You can use IN keyword 您可以使用IN关键字

SELECT COUNT(id)
FROM #__content
WHERE access IN ($niveis)

You can use find_in_set() : 您可以使用find_in_set()

select count(id)
from #__content
where find_in_set(access, $niveis) > 0;

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

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