0

I have the following tables:

list

  • id - key
  • name

item

  • id - key
  • name

list_item

  • list_id - foreign key to list table
  • item_id - foreign key to item table

I have the following query:

SELECT l.id, l.name
FROM list l, list_item li
WHERE l.id = li.list_id

I want to add to the result set, the count of items in the list. How do I do this?

4

2 に答える 2

4
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
于 2012-10-04T02:28:03.080 に答える
1

Try this:

SUM(l.id = li.list_id)
于 2012-10-04T02:28:02.680 に答える