user882670
user882670

Reputation:

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?

Upvotes: 0

Views: 80

Answers (2)

DevZer0
DevZer0

Reputation: 13525

You can use IN keyword

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

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269603

You can use find_in_set():

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

Upvotes: 0

Related Questions