Select multiple field duplicates from MySQL Database

后端 未结 1 584
忘掉有多难
忘掉有多难 2021-01-27 05:12

I\'ve got an old forum which contains threads with duplicate first posts (perhaps differing replies). I want to delete all but one of these threads (leaving the thread with the

相关标签:
1条回答
  • 2021-01-27 06:01

    A very hacky solution is to get the thread_id sorted by view_count in a GROUP_CONCAT. Then, we can utilize string operations to get the thread_id with minimum view_count.

    In your SELECT clause, instead of t.thread_id, you can try the following:

    SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT t.thread_id 
                                 ORDER BY t.view_count ASC 
                                 SEPARATOR ','), 
                    ',', 
                    1) AS thread_id_with_minimum_views
    

    Now, based on the SELECT query to identify Duplicate records with Minimum view, DELETE query to delete such records from the xf_thread table will be as follows:

    DELETE t_delete FROM xf_thread AS t_delete 
    INNER JOIN (SELECT CAST(SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT t.thread_id ORDER BY t.view_count ASC SEPARATOR ','), ',', 1) AS UNSIGNED) AS tid_min_view 
                FROM (SELECT * FROM xf_thread) t 
                INNER JOIN xf_post p ON p.thread_id = t.thread_id 
                WHERE t.first_post_id = p.post_id 
                  AND t.user_id = 0 
                  AND t.reply_count < 2 
                GROUP BY t.title, t.username, p.message 
                HAVING Count(t.title) > 1 
                   AND Count(t.username) > 1 
                   AND Count(p.message) > 1 
                ORDER BY t.thread_id) AS t_dup 
      ON t_delete.thread_id = t_dup.tid_min_view 
    
    0 讨论(0)
提交回复
热议问题