Bagaimana cara menghapus hanya beberapa data saja ? misal, hapus 2 data pertama yang sesuai dengan kriteria yang saya pilih ?
Karena di PostgreSQL tidak ada opsi LIMIT dalam klausa DELETE-nya, harus ada triks untuk melakukan hal ini, Alhamdulillah, hal ini dapat dilakukan dengan memanfaatkan ctid
DELETE FROM paragraph_soalApa itu ctid ? Dari [1] ctid adalah :
WHERE ctid IN (
SELECT ctid FROM paragraph_soal WHERE id_paragraph='xyz12345 LIMIT 1
);
The physical location of the row version within its table. Note that although the ctid can be used to locate the row version very quickly, a row's ctid will change each time it is updated or moved by VACUUM FULL. Therefore ctid is useless as a long-term row identifier. The OID, or even better a user-defined serial number, should be used to identify logical rows.
Contoh Lain :
select id_paragraph, isi, gambar from paragraph_soal where id_paragraph LIKE '17INA09%' group by id_paragraph, isi, gambar having count(*) > 1
DELETE FROM paragraph_soal
WHERE id_paragraph IN (SELECT id_paragraph
FROM (SELECT id_paragraph,
ROW_NUMBER() OVER (partition BY id_paragraph, isi, gambar ORDER BY id_paragraph) AS rnum
FROM paragraph_soal) t
WHERE t.rnum > 1);
Referensi
- 5.4. System Columns, https://www.postgresql.org/docs/8.2/static/ddl-system-columns.html
- Deleting duplicates, https://wiki.postgresql.org/wiki/Deleting_duplicates