How can I count matched rows individually?

Martin AJ

Here is my table structure:

-- qanda (stands for questions and answers)
+----+---------+-----------------------------------------------+--------------+-----------+------+
| id |  title  |                      content                  |  question_id |  user_id  | type |
+----+---------+-----------------------------------------------+--------------+-----------+------+
| 1  | title1  | this is a question                            | NULL         | 213423    | 0    |
| 2  | NULL    | this is an answer                             | 1            | 435344    | 1    |
| 3  | NULL    | this is another answer                        | 1            | 432435    | 1    |
| 4  | title2  | this is another question                      | NULL         | 124324    | 0    |
| 5  | NULL    | this is an answer for the second question     | 4            | 213423    | 1    |
| 6  | NULL    | this is another answer for the first question | 1            | 213423    | 1    |
+----+---------+-----------------------------------------------+--------------+-----------+------+

I want to count the number of question and answers individually. How can I do that?

The expected result for this user: :user_id = 213423

+--------+--------+
| q_num  | a_num  |
+--------+--------+
| 1      | 2      |
+--------+--------+

I can do that by two queries separately:

SELECT count(*) q_num FROM qanda WHERE user_id = :user_id AND question_id IS NULL

SELECT count(*) a_num FROM qanda WHERE user_id = :user_id AND question_id IS NOT NULL

Can I do that in one query?

Gordon Linoff

You can just do:

SELECT count(questionid) as q_num,
       sum(questionid is null) as a_num
FROM qanda
WHERE user_id = :user_id ;

count() with a column or expression counts the number of non-NULL values -- exactly what you want to do. MySQL treats boolean values as integers in a numeric context, with 1 for true and 0 for false.

You could also write this as:

(count(*) - count(questionid)) as a_num

or

sum(case when questionid is null then 1 else 0 end) as a_num

EDIT:

Using type, you can use a variant:

select sum(type = 0) as q_num, sum(type = 1) as a_num

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How I can count identical rows in sql

From Dev

How can I print string values individually?

From Dev

How can I scale Windows Individually?

From Dev

oracle sql count(*) with rows not matched

From Dev

SQL: How do I filter rows individually using a variable?

From Dev

How can I show a count of rows from an ng-repeat?

From Dev

How can I count rows matching criteria for each week in MySQL?

From Dev

How can I get a count of rows that match a given year in Rails?

From Dev

How can I count the numbers of rows that a phalcon query returned?

From Dev

How can I count rows base in several ranges in awk?

From Dev

How can I get the count of the visible rows of a grid?

From Dev

How can I count number of distinct rows of a table?

From Dev

How can I get a count of rows that match a given year in Rails?

From Dev

How can I get the count of the amount of rows for this result set in php?

From Dev

How can I count number of distinct rows of a table?

From Dev

tAggregateRow Talend - How can i count rows from table in Talend

From Dev

How can I count rows based on multiple different groups?

From Dev

How can I count all rows except duplicate entry of a column?

From Dev

How can I count the number of rows that are not zero in a certain range in python?

From Dev

How can I count different mysql rows in one database request?

From Dev

How can I get count of rows for every week?

From Dev

How can I count the number of rows in the last day?

From Dev

How can I inverse matched result of the pattern?

From Dev

How can I delete everything that is not matched by a pattern

From Dev

How can I edit each textbox line individually?

From Dev

How can I map the left and right shift key individually?

From Dev

How can I use dynamically generated variables from cfloop individually?

From Dev

How can I replace each group individually using Matcher?

From Dev

Using ember data how can I fetch children async individually?

Related Related

  1. 1

    How I can count identical rows in sql

  2. 2

    How can I print string values individually?

  3. 3

    How can I scale Windows Individually?

  4. 4

    oracle sql count(*) with rows not matched

  5. 5

    SQL: How do I filter rows individually using a variable?

  6. 6

    How can I show a count of rows from an ng-repeat?

  7. 7

    How can I count rows matching criteria for each week in MySQL?

  8. 8

    How can I get a count of rows that match a given year in Rails?

  9. 9

    How can I count the numbers of rows that a phalcon query returned?

  10. 10

    How can I count rows base in several ranges in awk?

  11. 11

    How can I get the count of the visible rows of a grid?

  12. 12

    How can I count number of distinct rows of a table?

  13. 13

    How can I get a count of rows that match a given year in Rails?

  14. 14

    How can I get the count of the amount of rows for this result set in php?

  15. 15

    How can I count number of distinct rows of a table?

  16. 16

    tAggregateRow Talend - How can i count rows from table in Talend

  17. 17

    How can I count rows based on multiple different groups?

  18. 18

    How can I count all rows except duplicate entry of a column?

  19. 19

    How can I count the number of rows that are not zero in a certain range in python?

  20. 20

    How can I count different mysql rows in one database request?

  21. 21

    How can I get count of rows for every week?

  22. 22

    How can I count the number of rows in the last day?

  23. 23

    How can I inverse matched result of the pattern?

  24. 24

    How can I delete everything that is not matched by a pattern

  25. 25

    How can I edit each textbox line individually?

  26. 26

    How can I map the left and right shift key individually?

  27. 27

    How can I use dynamically generated variables from cfloop individually?

  28. 28

    How can I replace each group individually using Matcher?

  29. 29

    Using ember data how can I fetch children async individually?

HotTag

Archive