sql 语句 通过表A id 查询表B

2024-12-02 22:34:35
推荐回答(5个)
回答(1):

这个肯定可以
select A.uid, SUM(CASE WHEN B.id IS NULL THEN 0 ELSE 1 END), A.name, A.age
from A left join B
ON A.uid = B.uid
group by A.uid, A.name, A.age

回答(2):

select v1.uid,isnull(v2.size,0) as size,v1.name,v1.age from 表A v1 left join (select uid,count(*) as size from 表B group by uid) v2 on v1.uid = v2.uid

回答(3):

select * from a left join b on a.uid=b.uid

回答(4):

select a.uid,a.name,a.age,isnull(b.cnt,0) as cnt
from [表A] as a
left join ( select uid,count(1) as cnt from [表B] group by uid ) as b on a.uid = b.uid

回答(5):

select a.uid, min(a.name) name, min(a.age) age, count(b.id) 人数 from a,b where a.uid=b.uid group by b.uid