mysql - 使用firstname / lastname交换查找重复的用户

时间:2011-10-12 02:14:02

标签: mysql duplicates

我需要找到已在表格中插入两次的用户,但使用他们的名字&姓氏交换。

e.g。 Bob Smith在数据库中

firstname:Bob 
lastname:Smith
&
firstname:Smith
lastname:Bob

找到这些用户的最佳查询是什么?

服务器运行mysql。

3 个答案:

答案 0 :(得分:2)

Select 
  *
FROM UserTable ut
JOIN UserTable ut2 on ut2.firstname = ut.lastname and ut2.lastname = ut.firstname

答案 1 :(得分:2)

SELECT
    firstname, lastname
FROM
    (
    SELECT firstname, lastname FROM MyTable -- WHERE firstname <> lastname
    UNION ALL
    SELECT lastname, firstname FROM MyTable -- WHERE firstname <> lastname
    ) foo
GROUP BY
   firstname, lastname
HAVING 
   COUNT(*) > 1

答案 2 :(得分:1)

SELECT orig.firstname, orig.lastname
FROM yourtable AS orig
INNER JOIN yourtable AS dupes ON orig.firstname = dupe.lastname AND orig.lastname = dupe.firstname

基本上,在用户表上进行自联接,但仅限于发生fn / ln欺骗交换的记录。