Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下表格:
列表
物品
项目清单
我有以下查询:
SELECT l.id, l.name FROM list l, list_item li WHERE l.id = li.list_id
我想将列表中的项目数添加到结果集中。我该怎么做呢?
SELECT l.id, l.name, COUNT(li.item_id) AS item_count FROM list l LEFT JOIN list_item ON l.id = li.list_id GROUP BY l.id
试试这个:
SUM(l.id = li.list_id)