select a from temp where nam='1'
union
select b from temp where nam='2'
union
select c from temp where nam='3'
union
select d from temp where nam='4'
union
select e from temp where nam='5'现在问题是如果某一条没有记录的时候就会少一行返回值,有没有什么办法当没有记录的时候能返回一个0
union
select b from temp where nam='2'
union
select c from temp where nam='3'
union
select d from temp where nam='4'
union
select e from temp where nam='5'现在问题是如果某一条没有记录的时候就会少一行返回值,有没有什么办法当没有记录的时候能返回一个0
解决方案 »
- 求SQL语句
- excel利用ADO保存数据到SQL数据库
- 在sql server 2000的查询分析器中如何使用 “sql server身份验证”登陆?
- sql生成主键问题!
- 受不了了,找了半天没找到问题.高手进来看看,应该超简单.
- 怎样把XLS表格倒入到SQLSERVER?
- 如何判断access数据库中一个表是否有回车键
- 请高手帮助根据性能监视器分析sql server 2000速度慢的原因
- 如何连接远程SQL Server2000
- 请问谁有SQL Server 2000的序列号 ,非常感激!!
- 老师们,进来看看,这是什么问题,奇怪!
- 怎么将SQL 2000 里将根目录由c:\...\MSSQL改成d:\MSSQL\
--这样?
--如果5这条没有记录
select 0 where not exists(select e from temp where nam='5')
--这样:select a from temp where nam='1'
union
select 0 where not exists(select a from temp where nam='1')
union
select b from temp where nam='2'
union
select 0 where not exists(select b from temp where nam='2')
union
select c from temp where nam='3'
union
select 0 where not exists(select c from temp where nam='3')
union
select d from temp where nam='4'
union
select 0 where not exists(select d from temp where nam='4')
union
select e from temp where nam='5'
union
select 0 where not exists(select e from temp where nam='5')
(
select a from temp where nam='1'
union
select b from temp where nam='2'
union
select c from temp where nam='3'
union
select d from temp where nam='4'
union
select e from temp where nam='5'
union
select 0
union
select 0
union
select 0
union
select 0
union
select 0
) t
select * from(
select a from temp where nam='1' union select 0 a where not exists(select a from temp where name='1'))
union all select * from(
select b from temp where nam='2' union select 0 where not exists(select b from temp where name='2'))
union all select * from(
select c from temp where nam='3' union select 0 where not exists(select c from temp where name='3'))
union all select * from(
select d from temp where nam='4' union select 0 where not exists(select d from temp where name='4'))
union all select * from(
select e from temp where nam='5' union select 0 where not exists(select e from temp where name='5'))
另这个方法笨了点,如果信息多点应该可以简化。
union
SELECT ISNULL((SELECT e FROM temp WHERE nam='5'),0)