0

我怎样才能左加入表 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
4

3 回答 3

3

一个简单的加入 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
于 2013-10-10T16:38:26.933 回答
0
SELECT
  concorsi.id,
  concorsi.name,
  COUNT(Aste.id) AS count
FROM
  concorsi
  LEFT JOIN Aste ON
    concorsi.id=Aste.concorsi_id
GROUP BY
  concorsi.id

有关 JOINS 如何工作的更多参考资料,请参阅此处的文章,并始终记得参考MySQL 手册

于 2013-10-10T16:38:31.167 回答
-1
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;
于 2013-10-10T16:42:16.543 回答