获取具有一对多表数的所有记录

时间:2014-07-04 09:40:51

标签: sql

我有两个表,一个是成员另一个是member_status表。我需要获得处于相同状态的成员数量。

构件

+----+-------------+-----------+---------+--------+--------+--------------+---------+---------------------+---------------------+
| id | full_name   | mobile_no | address | status | remark | edit_user_id | user_id | created_at          | updated_at          |
+----+-------------+-----------+---------+--------+--------+--------------+---------+---------------------+---------------------+
|  1 | John Doe    | 123       |         | 1      |        |            1 |       1 | 2014-06-19 15:51:08 | 2014-06-19 15:51:08 |
|  2 | Michael Bay | 123       |         | 1      |        |            1 |       1 | 2014-06-19 15:51:08 | 2014-06-19 15:51:08 |
|  3 | Hey Hey     | 123       |         | 3      |        |            1 |       1 | 2014-06-19 15:51:08 | 2014-06-19 15:51:08 |
+----+-------------+-----------+---------+--------+--------+--------------+---------+---------------------+---------------------+

member_status

+----+---------------------+----------------------+--------+--------------+---------+---------------------+---------------------+
| id | name                | description          | status | edit_user_id | user_id | created_at          | updated_at          |
+----+---------------------+----------------------+--------+--------------+---------+---------------------+---------------------+
|  1 | Visitor             | Visitor              | 1      |            1 |       1 | 2014-06-19 15:51:08 | 2014-06-19 15:51:08 |
|  3 | Member              | Member               | 1      |            1 |       1 | 2014-06-19 15:51:08 | 2014-06-19 15:51:08 |
|  4 | Level 1             | Level 1              | 1      |            1 |       1 | 2014-06-19 15:51:08 | 2014-06-19 15:51:08 |
| 11 | Level 12            | Level 12             | 1      |            1 |       1 | 2014-07-03 17:27:00 | 2014-07-03 17:27:00 |
+----+---------------------+----------------------+--------+--------------+---------+---------------------+---------------------+

我曾尝试过这个查询,但它不是正确的输出......

select `member_status`.`name`, count(*) as totalNum
from member
right join `member_status` on (member.`status` = `member_status`.id) 
group by `member_status`.name

这是输出我想拥有它

+---------------------+----------+
| name                | totalNum |
+---------------------+----------+
| Level 1             |        0 |
| Level 12            |        0 |
| Member              |        1 |
| Visitor             |        2 |
+---------------------+----------+

然而,不知何故,输出就是这个

+---------------------+----------+
| name                | totalNum |
+---------------------+----------+
| Level 1             |        1 |
| Level 12            |        1 |
| Member              |        1 |
| Visitor             |        2 |
+---------------------+----------+

有人可以提供建议吗?

4 个答案:

答案 0 :(得分:1)

SELECT S.NAME, SUM(CASE WHEN S.ID = T.STATUS THEN 1 ELSE 0 END ) FROM member_status S, member T GROUP BY S.NAME

答案 1 :(得分:1)

尝试
select * from member right join member_status on (member.status = member_status.id)
并且您将看到成员字段的NULL-s。 这就是为什么你得到更多的计数然后你想要的。

其中一个解决方案:
select member_status.name, count(member.id) as totalNum from member right join member_status on (member.status = member_status.id) group by member_status.name

select member_status.name, sum(case when member.id is null then 0 else 1 END) as totalNum from member right join member_status on (member.status = member_status.id) group by member_status.name

答案 2 :(得分:0)

您需要对此类结果进行相关子查询:

select      member_status.name, m.m_qty AS totalNum

from        member_status
left join   (
                select      member.status, count(member.id) AS m_qty
                from        member
                group by    member.status
            ) AS m ON (m.status = member_status.id) 

答案 3 :(得分:0)

这个怎么样:

select `member_status`.`name`, CASE WHEN member.`status` IS NULL THEN 0 ELSE count(*) as totalNum END
from member
right join `member_status` on (member.`status` = `member_status`.id) 
group by `member_status`.name, member.`status`