如何根据使用MySQL的第一个表中特定列中的值从2个表中获取记录数

时间:2018-04-21 05:59:05

标签: mysql sql count

这些是我从

获取的表格

寄存器

+----+-------------+--------+
| id | empSignupId | cityId |
+----+-------------+--------+
| 42 |           4 |      1 |
| 47 |           3 |      1 |
| 48 |          11 |      1 |
| 54 |          20 |      1 |
| 55 |          21 |      2 |
| 56 |          22 |      2 |
+----+-------------+--------+

guest_list

+-----+------------+-------------+
| id  | guestName  | empSignupId |
+-----+------------+-------------+
| 103 | Mallica SS |           3 |
| 104 | Kavya      |           3 |
| 108 | Vinay BR   |          11 |
| 109 |  Akash MS  |          11 |
+-----+------------+-------------+

城市

+----+---------------+
| id | cityName      |
+----+---------------+
|  1 | Bengaluru     |
|  2 | Chennai       |
|  3 | Sydney        |
|  4 | New York City |
|  5 | Shanghai      |
|  6 | Chicago       |
+----+---------------+

我需要提取从特定城市注册的人数,其中包括人,他们的客人,如果客人不在场,也应该显示人数。

这就是我试过的

SELECT COUNT(gl.id) +  COUNT(rfs.id), ct.cityName, rfs.cityId  
FROM register rfs 
INNER JOIN cities ct ON ct.id=rfs.cityId 
INNER JOIN guest_list gl ON gl.empSignupId = rfs.empSignupId 
GROUP BY rfs.cityId;

+-------------------------------+-----------+--------+
| COUNT(gl.id) +  COUNT(rfs.id) | cityName  | cityId |
+-------------------------------+-----------+--------+
|                             8 | Bengaluru |      1 |
+-------------------------------+-----------+--------+

我还需要显示来自其他城市的人数,因为没有来自某些城市的客人没有返回那个数字。

请帮我解决这个问题,我还是MySQL的新手..非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

你需要聚合guest_list表,以获得每empSignupId个记录的数量:

SELECT empSignupId, COUNT(empSignupId) AS countGuest
FROM guest_list gl 
GROUP BY empSignupId

<强>输出:

empSignupId countGuest
----------------------
3           2
11          2

现在你必须对上面的派生表使用LEFT JOIN,以便获得每个城市的记录数量:

SELECT COALESCE(SUM(countGuest), 0) + COUNT(rfs.id), ct.cityName, rfs.cityId  
FROM register rfs 
INNER JOIN cities ct ON ct.id=rfs.cityId 
LEFT JOIN (
  SELECT empSignupId, COUNT(empSignupId) AS countGuest
  FROM guest_list gl 
  GROUP BY empSignupId
) gl  ON gl.empSignupId = rfs.empSignupId 
GROUP BY rfs.cityId;

<强>输出:

COALESCE(SUM(countGuest), 0) + COUNT(rfs.id)    cityName    cityId
------------------------------------------------------------------
8                                               Bengaluru   1
2                                               Chennai     2

使用LEFT JOIN代替INNER JOIN可以保证我们也可以获得没有来宾的城市。

Demo here

注意:如果您还想让城市无法注册,那么您需要先放置cities表,然后使用LEFT JOINregister。< / p>

答案 1 :(得分:1)

使用LEFT JOINS并添加count(distinct r.empSignupId) + count(distinct g.id)

select 
    c.id as cityId,
    c.cityName,
    count(distinct r.empSignupId) + count(distinct g.id) as people_count
from cities c
left join register r on r.cityId = c.id
left join guest_list g on g.empSignupId = r.empSignupId
group by c.id;

结果将是:

| cityId |      cityName | people_count |
|--------|---------------|--------------|
|      1 |     Bengaluru |            8 |
|      2 |       Chennai |            2 |
|      3 |        Sydney |            0 |
|      4 | New York City |            0 |
|      5 |      Shanghai |            0 |
|      6 |       Chicago |            0 |

演示:http://rextester.com/OTBH14189

如果您不需要0的行,请将第一个LEFT JOIN更改为内部JOIN。