MySQL selects the desired account if the field is less than 0 and greater than 0

Is there a way to write a mysql select statement to start counting for only one field if it is greater than zero and equal to zero.

I could write 2 statements to achieve this, but is it possible to do this in one statement.

+3
source share
4 answers

Something like this is possible:

SELECT SUM(CASE WHEN x > 0 THEN 1 ELSE 0 END) as GreatherThanZero
    , SUM(CASE WHEN x = 0 THEN 1 ELSE 0 END) as EqualZero
FROM table
WHERE x >= 0
+14
source

Yes.

SELECT COUNT(*) FROM Table WHERE Field > 0
UNION SELECT COUNT(*) FROM Table WHERE Field = 0
+5
source

Yes.

SELECT SUM(CASE WHEN column > 0 THEN column ELSE 0 END CASE), SUM(CASE WHEN column < 0 THEN column ELSE 0 END CASE) FROM mytable
+1
source

Well, if I understood your question correctly, something like this should work:

SELECT
   COUNT(Field)
FROM
   Table
WHERE 
   Field >= 0
0
source

All Articles