sql – 在多个列中查找具有相同值的行

前端之家收集整理的这篇文章主要介绍了sql – 在多个列中查找具有相同值的行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试找到具有重复值的行,但仅基于选定列数,而不是单列或整行.例如,如果我的表格如下所示:
ID     Address    State    Name
-------------------------------
0      7 Brown    NY       John
1      3 Red      WX       Jane
2      7 Brown    WX       Ted
3      7 Brown    NY       Fred

我的问题是:

Find all ID’s for rows where the row’s Address and State field matched another row’s Address and State field.

这个查询的答案是:

ID    Address    State    Name
------------------------------
0     7 Brown    NY       John
3     7 Brown    NY       Fred

有任何想法吗?

建议:
How to select multiple columns values same rows from single table

解决方法

尝试以下:
SELECT A.*
FROM YourTable A
INNER JOIN (SELECT Address,State
            FROM YourTable
            GROUP BY Address,State
            HAVING COUNT(*) > 1) B
ON A.Address = B.Address AND A.State = B.State

猜你在找的MsSQL相关文章