标签: mysql sql database
你好我有两个数据库表,对于这个例子我使用表文章和评论。
这是架构:
有没有办法根据Comments计算FK_Article,然后在select表上计算值添加到标准Article查询?
Comments
FK_Article
select
Article
答案 0 :(得分:4)
聚合和COUNT应该足够了:
COUNT
SELECT A.ID, A.Date, COUNT(c.ID) AS Comments FROM Article A LEFT JOIN Comment c ON A.id = c.FK_Article GROUP BY A.ID, A.Date ORDER BY A.ID;