我怎样才能左加入表 Aste 所有帐户 concorso_id 等于表 concorsi 的 id ?
表协和
id name
1 car
2 bike
3 boat
表Aste
id concorso_id
1 1
2 1
3 2
4 2
5 1
6 3
我想要这个结果
id name count
1 car 3
2 bike 2
3 boat 1
一个简单的加入 group by 应该可以工作:
select c.id, c.name, count(a.*) total
from concorsi c
left join aste a on c.id = a.concorso_id
group by c.id
select c.id, c.name, count(concorso_id)
from table concorsi as c, Aste as a
where c.id=a.concorso_id group by concorso_id;