select a, b ,c from aaa where d='c' union all select 1,1,1 where not exists(select 1 from aaa where d='c')
树哥,这样是有问题的,select 没有FORM 不行吧
哥,这样可以的。没有FROM 也可以选出结果
测试了一下,没有FORM是没有问题, 但是有数据存在的时候会多出1,1,1的一行数据
if not OBJECT_ID('aaa') is null drop table aaa go CREATE table aaa (a char(10),b char(10),c char(10),d char(10)) insert into aaa select 'A','b','c','/' union all select 'A','c','c','/' union all select 'A','c','c','/' union all select 'A','c','c','/' go select a, b ,c from aaa where d='/' union all select '1','1','1' where not exists(select 1 from aaa where d='c') /* a b c A b c A c c A c c A c c 1 1 1 */
select * from 表 where 条件 if @@rowcount<=0 begin select 1,1,1 end --这样可以解决
条件不一样,所以会出现多一行,条件一样就不会出现select a, b ,c from aaa where d='/' union all select '1','1','1' where not exists(select 1 from aaa where d='/')
union all
select 1,1,1 where not exists(select 1 from aaa where d='c')
但是有数据存在的时候会多出1,1,1的一行数据
drop table aaa
go
CREATE table aaa (a char(10),b char(10),c char(10),d char(10))
insert into aaa
select 'A','b','c','/' union all
select 'A','c','c','/' union all
select 'A','c','c','/' union all
select 'A','c','c','/'
go
select a, b ,c from aaa where d='/'
union all
select '1','1','1' where not exists(select 1 from aaa where d='c')
/*
a b c
A b c
A c c
A c c
A c c
1 1 1
*/
if @@rowcount<=0
begin
select 1,1,1
end
--这样可以解决
union all
select '1','1','1' where not exists(select 1 from aaa where d='/')