SQL 查询没有正常工作
SQL Query not working as it should
所以我有三个表:
authors:
--------
ID Name
1 John
2 Sue
3 Mike
authors_publications:
---------------------
AuthorID PaperID
1 1
1 2
2 2
3 1
3 2
3 3
publications:
-------------
ID year
1 2004
2 2005
3 2004
我正在尝试加入他们的行列,以便我计算每个作者在 2004 年发表的出版物数量。如果他们没有发表任何内容,那么它应该为零
理想情况下,结果应如下所示:
ID Name Publications_2004
1 John 1
2 Sue 0
3 Mike 2
我尝试了以下方法:
select a.ID, Name, count(*) as Publications_2004
from authors_publications as ap left join authors as a on ap.AuthorID=a.ID left join publications as p on p.ID=ap.PaperID
where year=2004
group by ap.AuthorID
我不明白为什么它不起作用。它完全删除了 2004 年未发表的所有作者。
您的 WHERE 语句正在获取从 JOIN 返回的结果集,并且它们会剪掉 year<>2004
处的记录。
要解决这个问题,您可以做一些不同的事情
加入时,您可以在 ON
语句中对发布 table 应用过滤器。这将在加入之前过滤结果
SELECT a.ID,
NAME,
count(*) AS Publications_2004
FROM authors_publications AS ap
LEFT JOIN authors AS a
ON ap.AuthorID = a.ID
LEFT JOIN publications AS p
ON p.ID = ap.PaperID AND
p.year = 2004
GROUP BY ap.AuthorID
您可以使用 case 语句代替 WHERE:
SELECT a.ID,
NAME,
SUM(CASE WHEN p.year = 2004 THEN 1 ELSE 0) END AS Publications_2004
FROM authors_publications AS ap
LEFT JOIN authors AS a
ON ap.AuthorID = a.ID
LEFT JOIN publications AS p
ON p.ID = ap.PaperID
GROUP BY ap.AuthorID, NAME
您可以使用子查询将出版物 table 预过滤为仅 2004 条记录,这只是明确地执行第一个选项中隐含的内容:
SELECT a.ID,
NAME,
count(*) AS Publications_2004
FROM authors_publications AS ap
LEFT JOIN authors AS a
ON ap.AuthorID = a.ID
LEFT JOIN (SELECT * FROM publications WHERE AND year = 2004) AS p
ON p.ID = ap.PaperID
GROUP BY ap.AuthorID, NAME
此外,因为您没有使用公式聚合 NAME
,所以您应该将其添加到您的 GROUP BY
中,否则您可能会得到奇怪的结果。
所以我有三个表:
authors:
--------
ID Name
1 John
2 Sue
3 Mike
authors_publications:
---------------------
AuthorID PaperID
1 1
1 2
2 2
3 1
3 2
3 3
publications:
-------------
ID year
1 2004
2 2005
3 2004
我正在尝试加入他们的行列,以便我计算每个作者在 2004 年发表的出版物数量。如果他们没有发表任何内容,那么它应该为零
理想情况下,结果应如下所示:
ID Name Publications_2004
1 John 1
2 Sue 0
3 Mike 2
我尝试了以下方法:
select a.ID, Name, count(*) as Publications_2004
from authors_publications as ap left join authors as a on ap.AuthorID=a.ID left join publications as p on p.ID=ap.PaperID
where year=2004
group by ap.AuthorID
我不明白为什么它不起作用。它完全删除了 2004 年未发表的所有作者。
您的 WHERE 语句正在获取从 JOIN 返回的结果集,并且它们会剪掉 year<>2004
处的记录。
要解决这个问题,您可以做一些不同的事情
加入时,您可以在 ON
语句中对发布 table 应用过滤器。这将在加入之前过滤结果
SELECT a.ID,
NAME,
count(*) AS Publications_2004
FROM authors_publications AS ap
LEFT JOIN authors AS a
ON ap.AuthorID = a.ID
LEFT JOIN publications AS p
ON p.ID = ap.PaperID AND
p.year = 2004
GROUP BY ap.AuthorID
您可以使用 case 语句代替 WHERE:
SELECT a.ID,
NAME,
SUM(CASE WHEN p.year = 2004 THEN 1 ELSE 0) END AS Publications_2004
FROM authors_publications AS ap
LEFT JOIN authors AS a
ON ap.AuthorID = a.ID
LEFT JOIN publications AS p
ON p.ID = ap.PaperID
GROUP BY ap.AuthorID, NAME
您可以使用子查询将出版物 table 预过滤为仅 2004 条记录,这只是明确地执行第一个选项中隐含的内容:
SELECT a.ID,
NAME,
count(*) AS Publications_2004
FROM authors_publications AS ap
LEFT JOIN authors AS a
ON ap.AuthorID = a.ID
LEFT JOIN (SELECT * FROM publications WHERE AND year = 2004) AS p
ON p.ID = ap.PaperID
GROUP BY ap.AuthorID, NAME
此外,因为您没有使用公式聚合 NAME
,所以您应该将其添加到您的 GROUP BY
中,否则您可能会得到奇怪的结果。