How to display all the results of a MySQL query when using COUNT?

时间:2017-04-10 02:22:30

标签: mysql sql

I have the following:

SELECT field1,
       field2,
       field3,
       IF(field4 = 'valuex', COUNT(field4), 0) AS result
FROM tablex;`

But even though I have more records that I want to show, it only tells me all of the condition and shows them in a single record.

I need to get the other records of the table and the total value of the condition, not just a COUNT result.

1 个答案:

答案 0 :(得分:0)

You want the conditional as an argument to the SUM():

SELECT field1, field2, field3,
       SUM( field4 = 'valuex') AS result
FROM tablex;

This uses a convenient short-cut in MySQL. A boolean is treated as a number in a numeric context, with "1" for true and "0" for false.