sql查询where提交in的问题
时间:2011-11-24
来源:互联网
例如有一张表 a 字段有
id score
1 80
2 90
3 100
我要给予一个查询条件 select * from a where id in (1,2,3,4,5)
能否显示的结果为
1 80
2 90
3 100
4 0
5 0
表中不存在的也显示出来,分数默认为0
谢谢
要实现这种情况有哪些方法?程序或sql都行.
id score
1 80
2 90
3 100
我要给予一个查询条件 select * from a where id in (1,2,3,4,5)
能否显示的结果为
1 80
2 90
3 100
4 0
5 0
表中不存在的也显示出来,分数默认为0
谢谢
要实现这种情况有哪些方法?程序或sql都行.
作者: zhengbin215 发布时间: 2011-11-24
SQL code
;with cte as ( select 1 as row union select 2 union select 3 union select 4 union select 5 ) select a.row,isnull(b.score,0) score from cte a left join tb b on a.row = b.id
作者: AcHerat 发布时间: 2011-11-24
select b.id,isnull(a.score,0) as score
from
(select 1 as id union select 2 union select 3 union select 4 union select 5) b
left join a on a.id=b.id
from
(select 1 as id union select 2 union select 3 union select 4 union select 5) b
left join a on a.id=b.id
作者: FlySQL 发布时间: 2011-11-24
SQL code
create table tb(id int,score int) insert into tb select 1,80 insert into tb select 2,90 insert into tb select 3,100 go select a.number,isnull(b.score ,0)score from master..spt_values a left join tb b on a.number=b.id where type='p' and a.number in(1,2,3,4,5) /* number score ----------- ----------- 1 80 2 90 3 100 4 0 5 0 (5 行受影响) */ go drop table tb
作者: qianjin036a 发布时间: 2011-11-24
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28