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 列:ID 和 DocumentNumber。是一对多的关系,一个ID可以有多个证件号。
我需要获取属于它的所有 DocumentNumber 都是唯一的 ID。
这就是 Group By 与 Distinct 结合的目的吗?是否像按 ID 分组一样简单
您可以(正如您所怀疑的那样)使用简单的GROUP BY/HAVING和使用DISTINCT;
GROUP BY
HAVING
DISTINCT
SELECT id FROM documents GROUP BY id HAVING COUNT(DocumentNumber) = COUNT(DISTINCT DocumentNumber)
一个用于测试的 SQLfiddle。