我有3张桌子。我想按item.name可以分组计算idetail找到的次数。此查询仅计算所有行,不按行分组。' p7 N* r( `/ B% w |
有人知道怎么解决吗?% J# d B# v) ~' w d; V
SELECT i.name,COUNT(i.name)FROM item AS iINNER JOIN item_category AS ic ON i.i_category_id = ic.ic_idINNER JOIN idetail AS id ON ic.ic_id = id.id_category_idWHERE ic.ic_id = 1002GROUP BY i.name & w4 ?5 a3 D& ] T解决方案: * S+ J6 q7 l# J- \7 F! G9 a
这就是你想要的: 2 u+ s3 p" f x# Wselect x.name count(*) as cntNames sum(x.cntDetails) as cntDetails from ( SELECT i.name,COUNT(*) as cntDetails FROM item AS i INNER JOIN item_category AS ic ON i.i_category_id = ic.ic_id INNER JOIN idetail AS id ON ic.ic_id = id.id_category_id WHERE ic.ic_id = -- NOTICE THE 2nd Value in the group by! GROUP BY i.name,id.id_category_id ) x group by name