我想在一个sql语句中执行两个计数。

SELECT COUNT(UserID) AS STAFF
FROM User_Role
WHERE(RoleID <> 1)

UNION

SELECT COUNT(UserID) AS STUDENTS
FROM User_Role AS User_Role_1
WHERE (RoleID = 1)

上面的代码可以工作,但是输出如下:
Staff

7

6

不过,我也希望它以单独的颜色输出,例如:
Staff   Students
7          6

最佳答案

只需使用条件聚合。

SELECT sum(case when roleid<>1 then 1 else 0 end) AS STAFF,
       sum(case when roleid=1 then 1 else 0 end) AS STUDENTS
FROM User_Role

关于mysql - sql中有两个计数,但在单独的列中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36045640/

10-11 20:11