SQL Query无法正常工作

时间:2015-04-22 19:00:09

标签: sql python-2.7 sqlite left-join

所以我有三张桌子:

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年没有发表的任何作者。

1 个答案:

答案 0 :(得分:2)

您的WHERE语句正在从JOIN&#39;中返回结果集,并修剪year<>2004所在的记录。

要解决这个问题,你可以做一些不同的事情

您可以在加入时将过滤器应用于ON语句中的publications表。这将在加入

之前过滤结果
 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

您可以使用子查询将publication表预过滤到仅仅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,否则您可能会获得时髦的结果。