2016-06-21 62 views
2

我有如下查詢。FIND_IN_SET sql查詢

SELECT Id, FIND_IN_SET('22', Category) >0 AS `tot_cat` 
FROM tbl_doctorprofile 

我得到的結果1哪一行我得到的值「22」中的其他0 .. 這樣的結果是類似下面。

Id tot_cat 
---------- 
1 0 
2 0 
3 1 
4 0 

但我只想要擁有價值1

+0

顯示輸入法數據爲您的查詢 – Jens

回答

1

的行數試試這個:

SELECT Id, 
     COUNT(CASE WHEN FIND_IN_SET('22', Category) > 0 THEN 1 END) AS `tot_cat` 
FROM tbl_doctorprofile 
0

你想要嗎?

SELECT Id, FIND_IN_SET('22', Category) >0 AS `tot_cat` 
FROM tbl_doctorprofile 
HAVING `tot_cat` = 1 
2

所以只是總結列:

SELECT SUM(FIND_IN_SET('22', Category) >0) AS `tot_cat` 
FROM tbl_doctorprofile 

或正式更好一點:

SELECT COUNT(*) 
WHERE FIND_IN_SET('22', Category) > 0 
FROM tbl_doctorprofile 
1

您可以使用下面的查詢必須幫助你

SELECT COUNT(1) FROM 
(SELECT Id, FIND_IN_SET('22', Category) >0 AS `tot_cat` 
FROM tbl_doctorprofile) t WHERE t.tot_cat!=0;