Answer the question
In order to leave comments, you need to log in
How to compare COUNT for equality with 0 in a query?
There is such a request
SELECT dialogs.id_dialog AS id, clients.name AS name, COUNT( messages.status) AS qt
FROM dialogs INNER JOIN clients ON dialogs.id_client = clients.id_client INNER JOIN
messages ON dialogs.id_dialog = messages.id_dialog WHERE dialogs.id_manager = '1' AND
messages.status = '0' GROUP BY dialogs.id_dialog
IF (COUNT( messages.status) == '0') COUNT( messages.status) = 0
Answer the question
In order to leave comments, you need to log in
Here is the solution to my problem
SELECT dialogs.id_dialog AS id, clients.name AS name, SUM(case when messages.status = 0 then 1 else 0 end) AS qt
FROM dialogs INNER JOIN clients ON dialogs.id_client = clients.id_client INNER JOIN
messages ON dialogs.id_dialog = messages.id_dialog WHERE dialogs.id_manager = '1' GROUP BY dialogs.id_dialog
SELECT dialogs.id_dialog AS id, clients.name AS name, SUM(IF(messages.status = '0',0,1)) AS qt
FROM dialogs INNER JOIN clients ON dialogs.id_client = clients.id_client INNER JOIN
messages ON dialogs.id_dialog = messages.id_dialog WHERE dialogs.id_manager = '1' AND
messages.status = '0' GROUP BY dialogs.id_dialog
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question