2016-06-21 9 views
2

Ich habe Abfrage wie unten.FIND_IN_SET SQL Abfrage

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

Ich erhalte Ergebnis 1, in der Zeile i bin immer Wert '22' else 0 .. so ist Ergebnis wie unten.

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

Aber ich möchte nur Anzahl der Zeilen, die Wert 1.

+0

anzeigen Eingabehilfe muss Daten für Ihre Anfrage – Jens

Antwort

1

Try this:

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

Haben Sie dies wünschen?

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

So summieren nur die Spalte:

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

oder formal ein bisschen besser:

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

Sie unter Abfrage verwenden, kann es Ihnen

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