select Where id=? 时显示空值

aqgsh 2014-01-02 12:53:36
比如有一SQL语句:select * from jc_Paper where id in (1,2,3,4)
查询结果为:

而我想要的结果是这样的


请问各位位,SQL语句应该怎么写?谢谢!
...全文
206 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
aqgsh 2014-01-02
  • 打赏
  • 举报
回复
好的,谢谢两位。给了不错的思路,马上结贴,分不多,60分了,不好意思
發糞塗牆 2014-01-02
  • 打赏
  • 举报
回复
思路就是创建一个参照表,让sqlserver知道你要统计多少个ID,方式很多种,但是目的还是一个。我只是根据你的例子写代码,并不能完全满足你的要求。如果你的ID数量不多,用12楼的就够了,但是master库毕竟不能随便搞,如果真要实际运用,需要测试
發糞塗牆 2014-01-02
  • 打赏
  • 举报
回复
引用 11 楼 aqgsh 的回复:
[quote=引用 5 楼 DBA_Huangzj 的回复:] 因为你元数据里面“没有”2这个值,那么就要“造出来”,实际上就是搞个参照表,然后使用外联结,把没有匹配的数据也带出来,再处理,没什么简单的方法,如果不搞个参照表,sqlserver不知道你“想要”id为2的数据,参照表可以用我的方式或者你预先存起来在一个表里面
谢谢你的说明,感觉有点头绪了。40分少了,有点不好意思,一会加点分。 再请教一下,下面红色框的是什么意思,是不是表示如果4以后有空值,就不会列出来了 [/quote]这个是创建参照表,和12楼的master..spt_values类似,他那个表存了2000多个ID,如果你的ID超过这个数,还是要手动创建,如果你的id超过四,目前我的代码的确不会出来,只是给你一个思路而已
LongRui888 2014-01-02
  • 打赏
  • 举报
回复
再改一下:

select a.number as id,b.*
from master..spt_values a
left join  jc_Paper b 
       on a.number=b.id
where a.type = 'P' and a.number in (1,2,3,4)
aqgsh 2014-01-02
  • 打赏
  • 举报
回复
引用 5 楼 DBA_Huangzj 的回复:
因为你元数据里面“没有”2这个值,那么就要“造出来”,实际上就是搞个参照表,然后使用外联结,把没有匹配的数据也带出来,再处理,没什么简单的方法,如果不搞个参照表,sqlserver不知道你“想要”id为2的数据,参照表可以用我的方式或者你预先存起来在一个表里面


谢谢你的说明,感觉有点头绪了。40分少了,有点不好意思,一会加点分。
再请教一下,下面红色框的是什么意思,是不是表示如果4以后有空值,就不会列出来了
LongRui888 2014-01-02
  • 打赏
  • 举报
回复
额,又写错了,再改一下:
select a.number as id,b.*
from master..spt_values a
left join  jc_Paper b 
       on a.id=b.id
where a.type = 'P' and a.number in (1,2,3,4)
LongRui888 2014-01-02
  • 打赏
  • 举报
回复
引用 8 楼 aqgsh 的回复:
[quote=引用 6 楼 yupeigu 的回复:] [quote=引用 4 楼 aqgsh 的回复:] [quote=引用 2 楼 DBA_Huangzj 的回复:] 大概这样,其他字段我不写了
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-02 13:03:13
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[jc_Paper]
if object_id('[jc_Paper]') is not null drop table [jc_Paper]
go 
create table [jc_Paper]([id] int,[subjectid] varchar(4),[unitid] int)
insert [jc_Paper]
select 1,'A001',1 union all
select 3,'A001',1 union all
select 4,'A001',1
--------------开始查询--------------------------

select ISNULL(a.id,b.id)id,a.subjectid,a.unitid
 from [jc_Paper] a full join (select 1 id union all select 2 union all select 3 union all select 4) as b on a.id=b.id
 ORDER BY id
----------------结果----------------------------
/* 
id          subjectid unitid
----------- --------- -----------
1           A001      1
2           NULL      NULL
3           A001      1
4           A001      1

*/
你好,确实是可以的,但是这个我真的看不懂,知识太浅薄啊!! 能不能给我简单的说一下这是什么道理。或者有没有更简单一点的方法,谢谢![/quote] 试试这个,应该更加简单:
select a.number as id,b.*
from master..spt_values a
left join jc_pager b 
       on a.id=b.id
where a.type = 'P' and a.number in (1,2,3,4)
[/quote] 你好,你的方法报错了, 对象名 'jc_pager' 无效。[/quote] 呵呵,写错表名了:
select a.id,b.*
from master..spt_values a
left join  jc_Paper b 
       on a.id=b.id
where a.type = 'P' and a.number in (1,2,3,4)
aqgsh 2014-01-02
  • 打赏
  • 举报
回复
引用 6 楼 yupeigu 的回复:
[quote=引用 4 楼 aqgsh 的回复:] [quote=引用 2 楼 DBA_Huangzj 的回复:] 大概这样,其他字段我不写了
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-02 13:03:13
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[jc_Paper]
if object_id('[jc_Paper]') is not null drop table [jc_Paper]
go 
create table [jc_Paper]([id] int,[subjectid] varchar(4),[unitid] int)
insert [jc_Paper]
select 1,'A001',1 union all
select 3,'A001',1 union all
select 4,'A001',1
--------------开始查询--------------------------

select ISNULL(a.id,b.id)id,a.subjectid,a.unitid
 from [jc_Paper] a full join (select 1 id union all select 2 union all select 3 union all select 4) as b on a.id=b.id
 ORDER BY id
----------------结果----------------------------
/* 
id          subjectid unitid
----------- --------- -----------
1           A001      1
2           NULL      NULL
3           A001      1
4           A001      1

*/
你好,确实是可以的,但是这个我真的看不懂,知识太浅薄啊!! 能不能给我简单的说一下这是什么道理。或者有没有更简单一点的方法,谢谢![/quote] 试试这个,应该更加简单:
select a.number as id,b.*
from master..spt_values a
left join jc_pager b 
       on a.id=b.id
where a.type = 'P' and a.number in (1,2,3,4)
[/quote] 你好,你的方法报错了, 对象名 'jc_pager' 无效。
LongRui888 2014-01-02
  • 打赏
  • 举报
回复
这个其实就是,一个left join,以哪个为主表的问题,因为你的jc_pager里面没有id为2的记录,所以需要虚拟出一条2的记录。 所以这里以1,2,3,4的记录为主表,然后left join 你的jc_pager,就好了
LongRui888 2014-01-02
  • 打赏
  • 举报
回复
引用 4 楼 aqgsh 的回复:
[quote=引用 2 楼 DBA_Huangzj 的回复:] 大概这样,其他字段我不写了
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-02 13:03:13
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[jc_Paper]
if object_id('[jc_Paper]') is not null drop table [jc_Paper]
go 
create table [jc_Paper]([id] int,[subjectid] varchar(4),[unitid] int)
insert [jc_Paper]
select 1,'A001',1 union all
select 3,'A001',1 union all
select 4,'A001',1
--------------开始查询--------------------------

select ISNULL(a.id,b.id)id,a.subjectid,a.unitid
 from [jc_Paper] a full join (select 1 id union all select 2 union all select 3 union all select 4) as b on a.id=b.id
 ORDER BY id
----------------结果----------------------------
/* 
id          subjectid unitid
----------- --------- -----------
1           A001      1
2           NULL      NULL
3           A001      1
4           A001      1

*/
你好,确实是可以的,但是这个我真的看不懂,知识太浅薄啊!! 能不能给我简单的说一下这是什么道理。或者有没有更简单一点的方法,谢谢![/quote] 试试这个,应该更加简单:
select a.number as id,b.*
from master..spt_values a
left join jc_pager b 
       on a.id=b.id
where a.type = 'P' and a.number in (1,2,3,4)
發糞塗牆 2014-01-02
  • 打赏
  • 举报
回复
因为你元数据里面“没有”2这个值,那么就要“造出来”,实际上就是搞个参照表,然后使用外联结,把没有匹配的数据也带出来,再处理,没什么简单的方法,如果不搞个参照表,sqlserver不知道你“想要”id为2的数据,参照表可以用我的方式或者你预先存起来在一个表里面
aqgsh 2014-01-02
  • 打赏
  • 举报
回复
引用 2 楼 DBA_Huangzj 的回复:
大概这样,其他字段我不写了
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-02 13:03:13
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[jc_Paper]
if object_id('[jc_Paper]') is not null drop table [jc_Paper]
go 
create table [jc_Paper]([id] int,[subjectid] varchar(4),[unitid] int)
insert [jc_Paper]
select 1,'A001',1 union all
select 3,'A001',1 union all
select 4,'A001',1
--------------开始查询--------------------------

select ISNULL(a.id,b.id)id,a.subjectid,a.unitid
 from [jc_Paper] a full join (select 1 id union all select 2 union all select 3 union all select 4) as b on a.id=b.id
 ORDER BY id
----------------结果----------------------------
/* 
id          subjectid unitid
----------- --------- -----------
1           A001      1
2           NULL      NULL
3           A001      1
4           A001      1

*/
你好,确实是可以的,但是这个我真的看不懂,知识太浅薄啊!! 能不能给我简单的说一下这是什么道理。或者有没有更简单一点的方法,谢谢!
LongRui888 2014-01-02
  • 打赏
  • 举报
回复
试试这个:
select a.id,b.*
from (select 1 id union all select 2 union all select 3 union all select 4) a
left join jc_pager b 
       on a.id=b.id
          and b.id in (1,2,3,4)
    
發糞塗牆 2014-01-02
  • 打赏
  • 举报
回复
大概这样,其他字段我不写了
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-02 13:03:13
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[jc_Paper]
if object_id('[jc_Paper]') is not null drop table [jc_Paper]
go 
create table [jc_Paper]([id] int,[subjectid] varchar(4),[unitid] int)
insert [jc_Paper]
select 1,'A001',1 union all
select 3,'A001',1 union all
select 4,'A001',1
--------------开始查询--------------------------

select ISNULL(a.id,b.id)id,a.subjectid,a.unitid
 from [jc_Paper] a full join (select 1 id union all select 2 union all select 3 union all select 4) as b on a.id=b.id
 ORDER BY id
----------------结果----------------------------
/* 
id          subjectid unitid
----------- --------- -----------
1           A001      1
2           NULL      NULL
3           A001      1
4           A001      1

*/
發糞塗牆 2014-01-02
  • 打赏
  • 举报
回复
select * from jc_pager a full join (select 1 id union all select 2 union all select 3 union all select 4) as b on a.id=b.id在处理一下字段

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧