t1 t2
id name pt id name sign
1 a 10 1 XX 1
2 b 10 2 XX 1
3 c 10 3 XX 2
4 XX 1
5 XX 3
6 XX 2
7 XX 3
t1中的 id 与t2中的 sign关联。
希望找出 t1=10的数据如下:
id name count
1 a 3
2 b 2
4 c 2
------解决方案--------------------
- SQL code
select a.id,a.name,count(*) [count] from t1 ainner join t2 b on a.id=b.sign where a.pt=10 group by a.id,a.name
------解决方案--------------------
inner join ==>改為left join即可
------解决方案--------------------
将inner join改为left join即可.
------解决方案--------------------
- SQL code
select a.id,a.name,ISNULL(count(b.sign),0) [count] from t1 aleft join t2 b on a.id=b.sign where a.pt=10 group by a.id,a.name