mysql查询中单个列的条件

时间:2014-12-01 09:21:43

标签: mysql

我有一个表名'social_user'有一个名为'return'列的列的截图在下面 -

enter image description here

我想计算来自social_user表的用户,其中返回等于0作为new_user,其中return大于0作为returning_user,并且总用户输出应该像这个屏幕截图:

enter image description here

我尝试了下面的查询,但它不起作用 -

select count(`return` = 0) as new, count(`return` > 0) as returning from social_user

2 个答案:

答案 0 :(得分:1)

我猜你需要条件总和不计算

select
sum(`return` = 0) as `new`,
sum(`return` > 0) as returning,
count(*) as total_user
from social_user

答案 1 :(得分:1)

你可以这样做:

SELECT
    SUM(CASE WHEN `return` = 0 THEN 1 ELSE 0 END) AS new_user,
    SUM(CASE WHEN `return` > 0 THEN 1 ELSE 0 END) AS returning_user,
    SUM(CASE WHEN `return` >= 0 THEN 1 ELSE 0 END AS total_user
FROM
    social_user