简体   繁体   中英

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

I have the string $niveis with the following values (example):

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.

I tried the following query in PHP:

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

But it isn't working, can anyone help?

You can use IN keyword

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

You can use find_in_set() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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