Get 0 value from a count with no rows

后端 未结 6 1129

I have SELECT:

SELECT c FROM (
    SELECT "candidate_id" as id, count("candidate_id") as c
    FROM "Applicaions"
    GROUP BY &         


        
6条回答
  •  鱼传尺愫
    2020-12-14 17:50

    You can't.

    If your candidate has no applications, then you have no way to read their candidate_id value

    How would you have knowledge that a candidate exists without them being in the Applications table?

    In your example code, there is no candidate and therfore you couldn't possible say that a specific candidate had zero applications. By that logic there are an infinite number of candidates having zero applications.

    You will need a table of candidates in order to derive this information... unless your intention is to presume a candidate exists because you're asking for it by ID?

    EDIT

    Now that you have a Candidates table you can do this:

    SELECT c.ID, (SELECT COUNT(a.*) FROM Applications a WHERE a.candidate_id = c.ID) 
    FROM Candidate c
    WHERE ....
    

提交回复
热议问题