请教个sql 语句,可以实现吗

le616 2011-04-01 09:50:43
select count(t.id) num,t.id from table1 t where t.id in(1,2,3,4,5) group by id
------------
数据库中Id 实际只有1,2,3
----------
显示结果为
---------------------
num id
1 1
1 2
1 3
能不能这样显示
----------------
num id
1 1
1 2
1 3
0 4
0 5
...全文
118 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
O爱咋咋地O 2011-04-01
  • 打赏
  • 举报
回复
select count(t.id) num,t.id from table1 t where t.id in(1,2,3,4,5) group by id union all
select 0,id from table1 where id not in(1,2,3,4,5)
javatemptation 2011-04-01
  • 打赏
  • 举报
回复

use tempdb;
/*
create table table1
(
id int not null
);
insert into table1(id)
values(1),(2),(3);
*/
select
(select COUNT(id) from table1 where table1.id = t.id),t.id
from
(select 1 as id
union select 2
union select 3
union select 4
union select 5) as t;
le616 2011-04-01
  • 打赏
  • 举报
回复
sql 版大家太热心了 Thanks!
向各位高手学习!
--小F-- 2011-04-01
  • 打赏
  • 举报
回复
Group By一个不常用的用法



Flystone 2010-10-29于唐山


乍一看,大家一定认为这有什么好写的呀,不就是group by [item]嘛,对指定列进行分组而已。我在这里说的是一个将被MS删除、不符合ISO语法的用法(大家可以在MS没有删除前显摆显摆嘛,至少2008没有删除)。好,那这是什么语法呢,那就是Group by ALL 。那它和不加ALL有什么差别呢? 差别就是加了ALL后包含所有组和结果集,甚至包含那些其中任何行都不满足 WHERE 子句指定的搜索条件的组和结果集。注意的是指定了 ALL,将对组中不满足搜索条件的汇总列返回空值。
下面还是搞点数据来说明问题吧
---------------------------------------------------------------------

-- Author :flystone

-- Date : 2010-10-29 10:27:56

-- Version: Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86)

-- Mar 29 2009 10:27:29

-- Copyright (c) 1988-2008 Microsoft Corporation

-- Enterprise Edition on Windows NT 5.2 <X86> (Build 3790: Service Pack 2)

--

---------------------------------------------------------------------

declare @t table(id int,col char(2))

insert @t select 1,'a'

insert @t select 1,'a'

insert @t select 2,'a'

insert @t select 3,'a'

insert @t select 3,'a'

insert @t select 4,'a'

insert @t select 5,'a'

insert @t select 5,'a'

insert @t select 5,'a'

--1

select id,COUNT(1)

from @t

group by id

--2

select id,COUNT(1)

from @t

where id < 3

group by id

针对上面的数据我们可以得到显而意见的结果 :
/*

id

----------- -----------

1 2

2 1

3 2

4 1

5 3

(5 行受影响)

id

----------- -----------

1 2

2 1

(2 行受影响)

*/

那么如果我们想要得到如下结果呢?
/*

id

----------- -----------

1 2

2 1

3 0

4 0

5 0

*/

显然大家一看就知道我的意思了吧,常规有人一定会union联合、子查询什么的吧,有没有想过其实可以简单点呀,看看下面的语法:

--3

select id,COUNT(1)

from @t

where id < 3

group by all id

结果大家自己运行一下就有答案了。
最后后续版本的 Microsoft SQL Server 将删除该功能。请避免在新的开发工作中使用该功能,如果以后的项目里有使用的话还是着手修改当前还在使用该功能的应用程序吧。另外还得注意三点:1、CUBE 或 ROLLUP 运算符不能和ALL同时使用。2、如果在访问远程表的查询中还有 WHERE 子句,则该查询不支持 GROUP BY ALL。3、对于具有 FILESTREAM 属性的列,GROUP BY ALL将不被支持


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/happyflystone/archive/2010/11/04/5987769.aspx
快溜 2011-04-01
  • 打赏
  • 举报
回复
select count(t.id) num,t.id from table1 t where t.id in(1,2,3,4,5) group by id
select num=(select count(1) from table1 where id=a.id),a.id
from (select 1 as id union select 2 union select 3 union select 4 union select 5)a
dawugui 2011-04-01
  • 打赏
  • 举报
回复
select isnull(n.num) , m.id from
(
select 1 id union
select 2 id union
select 3 id union
select 4 id union
select 5 id
) m left join
(
select count(t.id) num,t.id from table1 t where t.id in(1,2,3,4,5) group by id
) n
on m.id = n.id

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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