日期:2014-05-17  浏览次数:20613 次

sql-查询赋值问题
假如 表名 为 table  结构如下

id  score
1   60 
2   50

这时 select * from table where id = 3  是没有记录的 
但是我想得到 
id  score
3   0
这样的结果
求 大神 帮帮忙啊(要一条语句搞定)

------解决方案--------------------

create table table1(id int, score int)

insert into table1
 select 1, 60 union all 
 select 2, 50


declare @id int
select @id=3

select @id 'id',
       isnull((select score 
               from table1
               where id=@id),0) 'score'

/*
id          score
----------- -----------
3           0

(1 row(s) affected)
*/


declare @id int
select @id=1

select @id 'id',
       isnull((select score 
               from table1
               where id=@id),0) 'score'

/*
id          score
----------- -----------
1           60

(1 row(s) affected)
*/