34,593
社区成员
发帖
与我相关
我的任务
分享
表 A
a b
1 B
2 C
表b
c d
5 6
5 7
5 8
表c
e f
6 8
7 9
10 11
结果集合
表 d
a b count
1 B 1=b表中d列小于7的个数
2 C 2=c表中f列小于11的个数
if object_id('A') is not null
drop table A
go
create table A
(
a int,
b varchar(10)
)
go
insert into A
select 1,'B' union all
select 2,'C'
go
if object_id('B') is not null
drop table B
go
create table B
(
c int,
d int
)
go
insert into B
select 5,6 union all
select 5,7 UNION ALL
select 5,8
go
if object_id('C') is not null
drop table C
go
create table C
(
e int,
f int
)
go
insert into C
select 6,8 union all
select 7,9 union all
select 10,11
go
select a,b,count=(select count(*) from B where d<7) from a where a=1
union all
select a,b,count=(select count(*) from C where f<11) from a where a=2
go
/*
a b count
----------- ---------- -----------
1 B 1
2 C 2
(2 行受影响)
*/