Wednesday 15 May 2019

MYSQL Delete all duplicate rows except one

  1. If you want to keep the row with the lowest id value:
    DELETE n1 FROM <table> n1, <table> n2 WHERE n1.id > n2.id AND n1.<column>= n2.<column>
  2. If you want to keep the row with the highest id value:
    DELETE n1 FROM <table> n1, <table> n2 WHERE n1.id < n2.id AND n1.<column> = n2.<column

No comments:

Post a Comment