select id,(select count(1) from tb where id = a.id) from ta a
create table ta(id int,col int) insert ta select 1,0 insert ta select 2,0 create table tb(id int)insert tb select 1 insert tb select 1 insert tb select 1 insert tb select 2 insert tb select 2 insert tb select 2 insert tb select 2 go select id,(select count(1) from tb where id = a.id) from ta a select a.id,count(b.id) from ta a full join tb b on a.id = b.id group by a.id /* id ----------- ----------- 1 3 2 4(所影响的行数为 2 行)id ----------- ----------- 1 3 2 4(所影响的行数为 2 行) 8/ drop table ta,tb
from ta a
insert ta select 1,0
insert ta select 2,0
create table tb(id int)insert tb select 1
insert tb select 1
insert tb select 1
insert tb select 2
insert tb select 2
insert tb select 2
insert tb select 2
go
select id,(select count(1) from tb where id = a.id)
from ta a
select a.id,count(b.id)
from ta a full join tb b on a.id = b.id
group by a.id
/*
id
----------- -----------
1 3
2 4(所影响的行数为 2 行)id
----------- -----------
1 3
2 4(所影响的行数为 2 行)
8/
drop table ta,tb