读书人

怎么写这样一个SQL语句

发布时间: 2012-01-07 21:41:55 作者: rapoo

如何写这样一个SQL语句
select a from table1 where(条件)
如果有记录显示查询出来的记录,如果没有记录显示“无”,如下所示

有记录: 无记录:
a a
---------- ----------
a1 无
a2
a3
...

[解决办法]
if exists(select 1 from table1)
select * from table1
else
select '无 ' from table1
[解决办法]
create table table1(a varchar(8))
insert table1 select 'a1 '
union all select 'a2 '
union all select 'a3 '
go

if exists(select * from table1 where a= 'a1 ')
select * from table1 where a= 'a1 '
else
select top 1 '无 ' as a from table1


if exists(select * from table1 where a= 'a4 ')
select * from table1 where a= 'a4 '
else
select top 1 '无 ' as a from table1

drop table table1

读书人网 >SQL Server

热点推荐