我有三张桌子。

Mark
id_mark
mark_name

example record:
1, 'Ford'
2, 'Fiat'

Model
id_model
id_mark
mondel_name

example record:
1, 1, 'Focus'
2, 2, 'Panda'

Adds
id_adds
id_model
name
price
etc.

example records:
1 1 'My ad', 20000
2 1 'My ad2', 30000
3 2 'My ad3', 30000

如何执行返回结果的查询(标记添加计数)示例:
    Ford 2
    Fiat 1

2 and 1 count

最佳答案

你需要两个接头。我已将第二个指定为LEFT OUTER JOIN,以便对于没有关联广告的make返回0。Mark通过Model连接到Adds

SELECT
  mark_name,
  COUNT(id_adds) AS num_ads
FROM
  Mark JOIN Model ON Mark.id_mark = Model.id_mark
  LEFT OUTER JOIN Adds ON Model.id_model = Adds.id_model
GROUP BY mark_name

10-05 20:11