作者joedenkidd (优质的蓝色射手)
看板Database
标题[SQL ] 数量统计问题?
时间Fri Jul 11 10:59:56 2014
Dear All
A Table(a)
name station
A station1
A station2
B station1
C station1
B Table (station1)
name qty
A 10
B 20
select count(*) from station1 group by name
结果:
A 10
B 20
但是
select distinct a.name,count(station1.*) from a left join station on a.name=station.name group by a.name
结果
A 20
B 20
看来是因为 a table name=A 出现两次
如果说用
select distinct a.name,count(station1.*) from a left join station on a.name=station.name where a.name='station1' group by a.name
A 10--->数量是对的
B 20
但我希望做到的是
1 从a table 找出所有不重复的 name
2 再用a.name到 station1.....station2 不同table做统计
在station1中
A 10
B 0
C 20
在station2中
A 0
B 10
C 10
而不是
station1
A 10
C 20
station2
B 10
C 10
麻烦大家帮忙解惑,谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.136.112.130
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Database/M.1405047598.A.911.html