Why is my left join not returning nulls?

后端 未结 6 1417
半阙折子戏
半阙折子戏 2020-12-05 18:31

In sql server 2008, I have the following query:

select      
    c.title as categorytitle,
    s.title as subcategorytitle,
    i.title as itemtitle
from cat         


        
6条回答
  •  清歌不尽
    2020-12-05 18:47

    Your "WHERE" criteria on i.siteid means that there has to be an "items" row in the output. you need to write (i.siteid is null or i.siteid = 132) or put the "i.siteid = 132" into the "ON" clause- something that will work for the itemcategories join too:

    select      
        c.title as categorytitle,
        s.title as subcategorytitle,
        i.title as itemtitle
    from categories c
    join subcategories s on c.categoryid = s.categoryid
    left join itemcategories ic on s.subcategoryid = ic.subcategoryid and ic.isactive = 1
    left join items i on ic.itemid = i.itemid and i.siteid = 132
    order by c.title, s.title
    

提交回复
热议问题