Left join with condition

后端 未结 6 1019
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 21:17

Suppose I have these tables

create table bug (
    id int primary key, 
    name varchar(20)
)
create table blocking (
    pk int primary key,
    id int, 
          


        
6条回答
  •  暖寄归人
    2020-12-29 22:07

    Here's a demo: http://sqlfiddle.com/#!2/414e6/1

    select
      bug.id,
      bug.name,
      blocking.name as blockingType
    from
      bug
        left outer join blocking on
          bug.id = blocking.id AND
          blocking.name = 'qa bug'
    order by
      bug.id
    

    By adding the "blocking.name" clause under the left outer join, rather than to the where, you indicate that it should also be consider "outer", or optional. When part of the where clause, it is considered required (which is why the null values were being filtered out).

    BTW - sqlfiddle.com is my site.

提交回复
热议问题