假设我有表A和表B,它们之间的关系是1到多,我如何只检索表B中A PK出现多次?的行?作为FK?我不知道我怎么能这样做。
答案 0 :(得分:1)
您可以对B
行进行分组,并在A
的子查询中对其进行计数:
SELECT *
FROM A
WHERE A.pk IN (SELECT fk
FROM B
GROUP BY fk
HAVING COUNT(*) > 1)
答案 1 :(得分:0)
SELECT fkey,count(*)FROM B WHERE fkey IN(SELECT pkey FROM A)GROUP BY fkey HAVING count(*)> 1;