读书人

合并sql语句?解决方法

发布时间: 2012-01-16 23:36:51 作者: rapoo

合并sql语句?
下面有2个sql,很简单:

select count(*) from a
select count(*) from a where id = 1

能不能使用一条语句实现上面的2条语句的功能,即同时查询所有的记录条数和符合某一条件的记录条数?

谢!

[解决办法]
select count(*),sum(case id when 1 then 1 else 0 end) from a
[解决办法]
select count(*) from a
union all
select count(*) from a where id = 1

[解决办法]
select count(*),num = (select count(*) from a where id = 1) from a

读书人网 >SQL Server

热点推荐