MYSQL - 查找并显示日期差异评论中的所有重复项

时间:2015-09-05 15:52:34

标签: mysql duplicates

下面的查询选择自早期注册以来具有相同父亲的行注册335天或更少的所有行。有没有办法编辑此查询,以便它不会消除输出中的重复行?我需要在335天之内看到该父亲的所有注册实例。

SELECT * FROM ymca_reg a later
WHERE NOT EXISTS (
    SELECT 1 FROM ymca_reg a earlier 
    WHERE 
        earlier.Father_First_Name = later.Father_First_Name
        AND earlier.Father_Last_Name = later.Father_Last_Name
        AND (later.Date - earlier.Date < 335) AND (later.Date > earlier.Date)

我目前的查询是:

SELECT ymca_reg.* FROM ymca_reg WHERE (((ymca_reg.Year) In (SELECT Year FROM ymca_reg As Tmp 
    GROUP BY Year, Father_Last_Name, Father_First_Name 
    HAVING Count(*)>1  
        And Father_Last_Name = ymca_reg.Father_Last_Name 
        And Father_First_Name = ymca_reg.Father_First_Name))) 
    ORDER BY ymca_reg.Year, ymca_reg.Father_Last_Name, ymca_reg.Father_First_Name 

此查询确实返回所有重复项以供正确查看,但它非常慢,因为它不使用连接,并且只要我添加日期条件,它就会返回后一行。感谢。

1 个答案:

答案 0 :(得分:0)

我想你想要这样的东西:

SELECT *
FROM ymca_reg later
WHERE EXISTS (SELECT 1
              FROM ymca_reg earlier 
              WHERE earlier.Father_First_Name = later.Father_First_Name AND
                    earlier.Father_Last_Name = later.Father_Last_Name AND
                    abs(later.Date - earlier.Date) < 335 and
                    later.Date <> earlier.Date
             );

这应该返回所有具有此类重复项的记录。请注意,“稍后”和“更早”不再是真正的描述,但我留下了名称,以便您可以看到与查询的相似性。