sql—如何使用mysql根据第1个表中特定列的值获取2个表中的记录数

eanckbw9  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(336)

这些是我要去拿伯爵的table
登记

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

客人名单

+-----+------------+-------------+
| 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的新手。。非常感谢您的帮助。

gfttwv5a

gfttwv5a1#

使用左连接和加法 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 ,将第一个左连接更改为内部连接。

vof42yt1

vof42yt12#

你需要聚合 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 保证我们的城市没有客人。
此处演示
注意:如果您还想获得没有注册的城市,那么您需要放置 cities 先用table,然后用 LEFT JOINregister .

相关问题