您可以在两个子查询连接上使用完全外部连接,如下所示
See Working demo
create table MainTable (idMain int);
insert into MainTable values (1),(2),(3),(4)
create table Child1 (idChild int);
insert into Child1 values (1),(1),(2),(3),(3),(3)
create table Child2 (idChild int);
insert into Child2 values (2),(2),(2),(3)
select
A.idMain,
ISNULL(countA,0) +ISNULL(countB,0) as count
from
(
select
M.idMain,count(1) countA
from MainTable M
join Child1 A
on A.idChild=M.idMain
group by M.idMain
)A
full outer join
(
select
M.idMain,count(1) countB
from MainTable M
join Child2 B
on B.idChild=M.idMain
group by M.idMain
)B
on A.idMain=B.idMain
--- your where clause goes here