Mysql 带条件的左连接

Mysql 带条件的左连接,mysql,sql,tsql,left-join,Mysql,Sql,Tsql,Left Join,假设我有这些桌子 create table bug ( id int primary key, name varchar(20) ) create table blocking ( pk int primary key, id int, name varchar(20) ) insert into bug values (1, 'bad name') insert into bug values (2, 'bad condition') insert

假设我有这些桌子

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

insert into bug values (1, 'bad name')
insert into bug values (2, 'bad condition')
insert into bug values (3, 'about box')
insert into blocking values (0, 1, 'qa bug')
insert into blocking values (1, 1, 'doc bug')
insert into blocking values (2, 2, 'doc bug')
我想连接id列上的表,结果如下:

id          name                 blockingName
----------- -------------------- --------------------
1           bad name             qa bug
2           bad condition        NULL
3           about box            NULL
select * from #bug t1 
    left join #blocking t2 on t1.id = t2.id
    where t2.name is null or t2.name = 'qa bug'
这意味着: 我想返回bug中的所有行 “blockingName”列中应该只有“qa bug”值,如果在blocking中找不到匹配的行,则应为NULL

我天真的选择是这样的:

id          name                 blockingName
----------- -------------------- --------------------
1           bad name             qa bug
2           bad condition        NULL
3           about box            NULL
select * from #bug t1 
    left join #blocking t2 on t1.id = t2.id
    where t2.name is null or t2.name = 'qa bug'
但这不起作用,因为似乎先将条件应用于阻塞表,然后将其联接


这个问题的最简单/典型解决方案是什么?我有一个嵌套选择的解决方案,但我希望有更好的解决方案,看起来您只想从阻塞中选择一行,并将其连接到bug。我会:

select t1.id, t1.name, t2.name as `blockingName` 
from `#bug` t1
left join (select * from `#blocking` where name = "qa bug") t2
on t1.id = t2.id

只需将qa错误标准放入联接:

select t1.*, t2.name from #bug t1 
left join #blocking t2 on t1.id = t2.id AND t2.name = 'qa bug'

确保内部查询只返回一行。 如果返回多个,则可能必须在其上添加一个top 1

select 
t1.id, t1.name,
(select  b.name from #blocking b where b.id=t1.id and b.name='qa bug')
from #bug t1 
正确的选择是:

create table bug (
id int primary key, 
name varchar(20)
)
insert into bug values (1, 'bad name')
insert into bug values (2, 'bad condition')
insert into bug values (3, 'about box')

CREATE TABLE blocking
(
pk int IDENTITY(1,1)PRIMARY KEY ,
id int, 
name varchar(20)
)
insert into blocking values (1, 'qa bug')
insert into blocking values (1, 'doc bug')
insert into blocking values (2, 'doc bug')


select 
t1.id, t1.name,
(select  b.name from blocking b where b.id=t1.id and b.name='qa bug')
from bug t1 
下面是一个演示:

通过在左侧外部连接中添加Cubj.NEX子句,而不是将其添加到何处,指示它也应该考虑外部的或可选的。当它是where子句的一部分时,它被认为是必需的,这就是筛选空值的原因


顺便说一句,sqlfiddle.com是我的网站。

MySQL是首选,但T-SQL也可以。你想实现什么?是不是所有的bug都没有拦截器或者有“qa bug”拦截器?首先,您的第2行不会返回NULL,因为它在阻塞中有一个匹配的行。其次,您使用的是什么SQL?您有一个mysql标记,但这是无效的mysql语法,并且避免了您的查询在mysql中工作,请参见。PK对我来说并不重要,但在这里添加了一些。忘记我关于第2行的第一句话;我误解了你。