使用LIKE子句计算每个外键的记录数

时间:2014-04-02 19:00:22

标签: sql sqlite select count sql-like

My image, explanation

  • 我需要计算不存在的外键,如图所示,并在列表名称中包含字母“a”。
  • 我只能计算值,但无法用字母“a”指定名称。

我正在使用计数:

SELECT l.identifier AS id, l.name AS listName, COUNT(p.list_identifier)
AS regCount FROM list AS l LEFT JOIN person AS p ON l.identifier = p.list_identifier
WHERE p.list_identifier IS NULL GROUP BY l.name;

请帮帮我。

1 个答案:

答案 0 :(得分:0)

尝试使用left outer join& COUNT(DISTINCT ...)

SELECT COUNT(DISTINCT l.identifier) AS regCount
FROM list AS l LEFT OUTER JOIN person AS p ON l.identifier = p.list_identifier
WHERE l.Name LIKE '%a%'