日期:2014-05-17 浏览次数:20374 次
select COUNT(*) as 总记录,(select COUNT(*) from b where a.id=b.a_id) as 安装记录数
from a
;with a(id,name) as
(
select 1,'name1'
union all select 2,'name2'
),
b(id,a_id) as
(
select 1,1
)
select COUNT(*) as 总记录,(select COUNT(b.a_id) from b inner join a on a.id=b.a_id) 安装记录数
from a
/*
总记录 安装记录数
2 1
*/
if object_id('Tempdb..#a') is not null drop table #a
if object_id('Tempdb..#b') is not null drop table #b
create table #a(
[id] int identity(1,1) not null,
[name] nvarchar(100) null
)
create table #b(
[id] int identity(1,1) not null,
[a_id] int null
)
--a表插入6条记录
Insert Into #a
select 'name1' union all
select 'name2' union all
select 'name3' union all
select 'name4' union all
select 'name5' union all
select 'name6'