SQLの表の同じ列で重複した値を削除する


時々、テーブルは文字列、数またはetcとして複製値を持ちます.そして、我々はそのデータをきれいにしたいです.しかし、データの動的なので、テーブルのすべてのケースのハードコードをすることはできません.

fruits table


ID
名称
1
オレンジ🍊
2
アップル🍎
3
オレンジ🍊
4
パイナップル🍍
この場合、以下のような単純なSQLを持っています.
DELETE
  FROM fruits a
 USING fruits b
 WHERE a.id > b.id
   AND a.name = b.name;

Result after excute query


ID
名称
1
オレンジ🍊
2
アップル🍎
4
パイナップル🍍

If several columns have the same names but the datatypes do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an EQUIJOIN.


Find out USING in this article
楽しむ時間🪴
読書ありがとう.