这个有测试数据:--> 测试数据: @t1
declare @t1 table (c11 int,c12 int)
insert into @t1
select 1,11 union all
select 2,22
--> 测试数据: @t2
declare @t2 table (c21 int,c22 int)
insert into @t2
select 1,111select (SELECT CASE WHEN c22 IS NULL THEN '否' ELSE '是' END FROM @t2 where t1.c11 = c21) from @t1 t1
(所影响的行数为 2 行)
(所影响的行数为 1 行)
----
是
NULL ------这里是NULL,不是否(所影响的行数为 2 行)
1,为什么结果会是NULL,不是“否”
2,怎样能让结果变成“否”
declare @t1 table (c11 int,c12 int)
insert into @t1
select 1,11 union all
select 2,22
--> 测试数据: @t2
declare @t2 table (c21 int,c22 int)
insert into @t2
select 1,111select (SELECT CASE WHEN c22 IS NULL THEN '否' ELSE '是' END FROM @t2 where t1.c11 = c21) from @t1 t1
(所影响的行数为 2 行)
(所影响的行数为 1 行)
----
是
NULL ------这里是NULL,不是否(所影响的行数为 2 行)
1,为什么结果会是NULL,不是“否”
2,怎样能让结果变成“否”
declare @t1 table (c11 int,c12 int)
insert into @t1
select 1,11 union all
select 2,22
--> 测试数据: @t2
declare @t2 table (c21 int,c22 int)
insert into @t2
select 1,111select '是' from @t1 t1 where exists (select 1 from @t2 t2 where t2.c21 = t1.c11)
union all
select '否' from @t1 t1 where not exists (select 1 from @t2 t2 where t2.c21 = t1.c11)/*
----
是
否(所影响的行数为 2 行)
*/
case
when (SELECT CASE WHEN c22 FROM @t2 where t1.c11 = c21) IS NULL
THEN '否'
ELSE '是'
END
from @t1 t1
from @t1 t1
from @t1 t1
declare @t1 table (c11 int,c12 int)
insert into @t1
select 1,11 union all
select 2,22
--> 测试数据: @t2
declare @t2 table (c21 int,c22 int)
insert into @t2
select 1,111select CASE WHEN (SELECT c22 FROM @t2 where t1.c11 = c21) IS NULL THEN '否' ELSE '是' END from @t1 t1
、*----
是
否(2 行受影响)
*、
declare @t1 table (c11 int,c12 int)
insert into @t1
select 1,11 union all
select 2,22
--> 测试数据: @t2
declare @t2 table (c21 int,c22 int)
insert into @t2
select 1,111select m.* , case when c22 is null then '否' else '是' end
from @t1 m left join @t2 n on m.c11 = n.c21/*
c11 c12
----------- ----------- ----
1 11 是
2 22 否(所影响的行数为 2 行)*/
declare @t1 table (c11 int,c12 int)
insert into @t1
select 1,11 union all
select 2,22
--> 测试数据: @t2
declare @t2 table (c21 int,c22 int)
insert into @t2
select 1,111select isnull((SELECT CASE WHEN c22 IS NULL THEN '否' ELSE '是' END FROM @t2 where t1.c11 = c21),'否')
from @t1 t1
/*----
是
否(2 行受影响)
*/
FROM @t2 Right JOIN @t1 t1 ON t1.c11 = c21
/*
----
是
否(2 行受影响)*/
case when b.c21 is null then '否' else '是' end
from @t1 a
left join @t2 b on a.c11=b.c21