簡體   English   中英

使用字符串作為條件計算SQL中的行數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM