P
P
Pavel2020-04-24 13:55:43
MySQL
Pavel, 2020-04-24 13:55:43

How to add condition in mysql query to count rows?

I have such a request

SELECT user_id, dogovor_name, COUNT(*) AS cnt
                    FROM user_dolg
                    WHERE dogovor_name='обыкновенный'
                    GROUP BY user_id

counts clearly, but it is necessary that if no matches are found, put in cnt 0, how to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-04-24
@mrusklon

I wrote on my knee, I need to check ...

SELECT user_id, 'обыкновенный' dogovor_name, SUM(case when dogovor_name='обыкновенный' then 1 else 0 end) AS cnt
                    FROM user_dolg
                    GROUP BY user_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question