Answer the question
In order to leave comments, you need to log in
Count number of rows under different conditions in one query?
Hello everyone, please tell me there is a table with comments, you need to combine 2 such simple queries into one:
SELECT COUNT(id) as one_count FROM comments WHERE user_id = $user_id AND post_id != 1
SELECT COUNT(id) as two_count FROM comments WHERE user_id = $user_id AND post_id = 1
Answer the question
In order to leave comments, you need to log in
SELECT COUNT(id) as `count`, "one" as `name` FROM comments WHERE user_id = $user_id AND post_id != 1
UNION
SELECT COUNT(id) as `count`, "two" as `name` FROM comments WHERE user_id = $user_id AND post_id = 1
count | name
=============
12 | one
234 | two
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question