简体   繁体   中英

MySQL select need count if field less than 0 and greater than 0

Is there a way to write a mysql select statement to run a count for a particular 1 field only, if greater than zero and equal to zero.

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

Something like this perhaps:

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

Yes.

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

是。

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM