3个表上的SQL查询(计数)

时间:2015-02-07 16:34:21

标签: mysql sql join left-join

我有三个表:S(员工),C(客户),O(操作)。以下是表格的屏幕截图。 enter image description here

enter image description here

enter image description here

如何为每个城市打印客户数量和居住在那里的员工人数? 因此,该表将有3列:City,Count(Clinets),Count(Stuff)

1 个答案:

答案 0 :(得分:2)

select city, sum(clients) as clients, sum(staff) as staff
from 
(
  select city, count() as clients, null as staff from clients group by city
  union all 
  select city, null, count() from staff group by city
) tmp
group by city