Ferzinha Fil
Ferzinha Fil

Reputation: 142

SQLite - Count how many records there are for each foreign key

I'm using:

SELECT l.name AS listName, COUNT(p.fk_list_identifier) AS countNum
FROM list AS l INNER JOIN person AS p 
WHERE l.identifier = p.fk_list_identifier
GROUP BY l.name;

Upvotes: 0

Views: 531

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270623

If you just want the lists with no reference, you can do:

SELECT l.name AS listName
FROM list l LEFT JOIN
     person AS p 
     on l.identifier = p.fk_list_identifier
WHERE p.fk_list_identifier IS NULL
GROUP BY l.name;

Upvotes: 2

Related Questions