首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么写这样一个SQL语句

2012-01-07 
如何写这样一个SQL语句selectafromtable1where(条件)如果有记录显示查询出来的记录,如果没有记录显示“无”,

如何写这样一个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

热点排行