Advertisement
5ally

Untitled

Nov 9th, 2018
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.07 KB | None | 0 0
  1. # NOTE
  2. # The records will be sorted by the post date, in ASCending order (newest to
  3. # oldest), which means posts created later are assumed as duplicates. Example
  4. # cases, where post-1 and post-2 have the *same slug* and created by the *same author*:
  5. #   1) post-1 created on Nov 1st; post-2 created on Nov 2nd; so post-2 is a duplicate.
  6. #   2) Same as #1 above, but post-2 was updated on Nov 3rd; so post-1 is a duplicate.
  7. # because the column post_date is changed when the post is created/updated.
  8.  
  9. # Outer query - Select duplicate records.
  10. SELECT p.ID, p.post_author, p.post_name, p.post_type, p.post_status
  11. FROM wp_posts p
  12.     INNER JOIN wp_posts p2 ON p2.post_author = p.post_author
  13. WHERE p.ID NOT IN (
  14.     # Sub-query - Select original records.
  15.     SELECT ID
  16.     FROM wp_posts
  17.     GROUP BY post_author, post_name
  18.     HAVING ( COUNT(post_author) > 1 )
  19.         AND ( COUNT(post_name) > 1 )
  20.     # This should match the outer's ORDER BY
  21.     ORDER BY post_date ASC
  22. )
  23.     AND p.ID != p2.ID
  24.     AND p.post_name = p2.post_name
  25. # This should match the sub-query's ORDER BY
  26. ORDER BY p.post_date ASC
  27. LIMIT 15
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement