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

sql-查询赋值有关问题

2013-06-25 
sql-查询赋值问题假如 表名 为 table结构如下idscore160 250这时 select * from table where id 3是没有

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)
*/

热点排行