我有这张桌子:
我正在使用此查询:
select ID fromMytable were FK <> 1
group by ID, FK
order by ID
这给了我想要的结果:
255是重复的,因为有两个不同的KF。其余的ID具有相同的FK。我想获得具有多个FK且具有不同值的ID。
如果ID有两行FK = 2且FK = 3则获取此ID,但如果ID为FK = 2,FK = 2,则FK = 2我不想要此ID,因为它有同样的FK。
我怎么能得到这个ID?
非常感谢你。
答案 0 :(得分:4)
您应该计算不同的FK
s
select ID from Mytable where FK <> 1
group by ID
having count(distinct FK) > 1
order by ID
答案 1 :(得分:0)
使用HAVING
仅查找多出一次的ID:
select DISTINCT ID
from Mytable
where FK <> 1
group by ID, FK
having count(*) >= 2
order by ID
答案 2 :(得分:0)
试试这个:
SELECT
ID, COUNT(*)
FROM
fromMytable
WHERE
FK <> 1
GROUP BY
ID
HAVING
COUNT(*) > 1
ORDER BY ID
答案 3 :(得分:0)
您可以使用ROW_NUMBER
窗口功能。
SELECT ID FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) RN, ID
from Mytable WHERE FK <> 1
) TMP
WHERE RN = 1