SQL - 计算每列= 1的每列

时间:2016-07-11 04:00:07

标签: mysql

我想按组织统计老师,组织者,管理员小组。

这是我当前的数据库示例:

enter image description here

预期结果将是:

teacher | organizer | administrator | organization_id
---------------------------------------------------
    1   |   1       |       1       |       6035
    5   |   4       |       1       |       12554

到目前为止我尝试了什么

SELECT
    count(teacher),
    count(organizer),
    count(administrator),
    organization_id
FROM users
WHERE
    teacher = 1
AND organizer = 1
AND administrator = 1
GROUP BY organization_id;

2 个答案:

答案 0 :(得分:1)

因为您的列值为0/1,因此您只需使用SUM来计算nums:

SELECT
    SUM(teacher) as teacher,
    SUM(organizer) as organizer,
    SUM(administrator)as administrator,
    organization_id
FROM users GROUP BY organization_id

答案 1 :(得分:0)

根据您的预期结果,我认为您应该在AddType image/svg+xml svg AddType image/svg+xml svgz 声明中使用or而不是and

where