Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. DELETE FROM users WHERE id IN (
  2. SELECT id FROM (
  3. SELECT * FROM users a
  4. LEFT JOIN
  5. (SELECT Max(id) max_id FROM users GROUP BY duplic_col) b
  6. ON a.id = max_id
  7. WHERE max_id IS NULL
  8. ) AS select_id
  9. );
  10. -- Successfully Tested!
  11. -- ------------------------------------------------------------------
  12.  
  13. DELETE a
  14. FROM users a
  15. LEFT JOIN(SELECT Max(date_col) max_date,
  16. col_1,
  17. col_2
  18. FROM users
  19. GROUP BY col_1,
  20. col_2) b -- Select Max-Date for every group
  21. ON a.date_col = max_date -- Left join 'users' with Select Max-Date
  22. AND a.col_1 = b.col_1
  23. AND a.col_2 = b.col_2 -- Compare by another columns
  24. WHERE b.max_date IS NULL; -- Where date_col != max_date
  25. -- -------------------------------------------------------------------
  26.  
  27. DELETE FROM comments WHERE CommentId IN (
  28. SELECT CommentId FROM
  29. (SELECT * FROM comments a
  30. LEFT JOIN
  31. (SELECT Max(CommentId) max_id FROM comments GROUP BY CommentZendeskId ) b
  32. ON a.CommentId = max_id
  33. WHERE a.CommentZendeskId != 0 AND b.max_id IS NULL
  34. AND CreatedAt >= '2019-05-21 11:20'
  35. ) AS x
  36. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement