sql question - outer join seems not to work

前端 未结 3 804
无人共我
无人共我 2021-01-24 06:38

using: sql server

Here is what I am trying to accomplish. I have two tables one with 70 companies (company info, etc) and one table recording the incident numbers of tho

3条回答
  •  礼貌的吻别
    2021-01-24 06:59

    If the WHERE clause is against the outer table in the JOIN, it effectively makes the JOIN an INNER JOIN (this is actually a very common mistake). It's hard to tell which table your WHERE clause criteria works against, since you don't qualify the tables.

    However, my guess is you need to make the WHERE clauses a condition of the JOIN. Something like this:

    SELECT 
        count(*) AS total_num, 
        TS_NAME 
    FROM 
        TTS_INCIDENTS 
        LEFT OUTER JOIN TS_COMPANIES ON 
            TS_COMPANIES.TS_ID=TTS_INCIDENTS.TS_COMPANYID 
            AND TS_ACTIVEINACTIVE = 0 AND (TS_INCIDENTTYPE=10 OR TS_INCIDENTTYPE=11) 
    GROUP BY 
        TS_NAME 
    ORDER BY 
        TS_NAME 
    

提交回复
热议问题