Mysql 如何从两个表中获取发生次数
我有两个表:Mysql 如何从两个表中获取发生次数,mysql,sql,count,Mysql,Sql,Count,我有两个表:项和项。Items有很多东西 项目 事物 id name item_id ---- ----- ----------- 1 thing1 1 2 thing2 1 3 thing3 1 4 thing6 2 5 thing7 6 6 thing6 2 我希望返回一个结果,根据item\u id id name count --- ---- ------ 1 item1
项
和项
。Items有很多东西
项目
事物
id name item_id
---- ----- -----------
1 thing1 1
2 thing2 1
3 thing3 1
4 thing6 2
5 thing7 6
6 thing6 2
我希望返回一个结果,根据item\u id
id name count
--- ---- ------
1 item1 3
2 item2 2
试试这个:
SELECT i.id,i.name,count(*)
FROM items i
INNER JOIN things t ON t.item_id = i.id
GROUP BY i.id, i.name
ORDER BY i.id
SELECT i.id,i.name,count(*)
FROM items i
INNER JOIN things t ON t.item_id = i.id
GROUP BY i.id, i.name
ORDER BY i.id