Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有 2 张桌子我想加入和..
A: ID Otherfields.. 1 ... 2 3 4 B: ID aId Otherfields.. 1 1 ... 2 1 3 2 4 1
所以我完全有能力通过 a.Id 加入他们,但是如何获得表 B 中匹配项的计数,例如:
a.id count(b) 1 3 2 1
我想它一定是 count() over() 的东西,但不记得确切的用途。
谢谢!
你可以简单地这样做:
SELECT A.ID, COUNT(b.ID) FROM A INNER JOIN B ON A.Id = b.aID GROUP BY A.ID
你可以有
SELECT A.ID, COUNT(b.ID) FROM A LEFT JOIN B ON A.Id = b.aID GROUP BY A.ID
这将为您提供所有a.ID不存在的 s,b.ID因此将其计数显示为 0。
a.ID
b.ID
例如,
ID Count 1 3 2 1 3 0 4 0