Nah, Anda semacam menjawab pertanyaan Anda. Anda sepertinya ingin max(id)
:
SELECT email, COUNT(email) AS occurences, max(id)
FROM wineries
GROUP BY email
HAVING (COUNT(email) > 1);
Anda dapat menghapus yang lain menggunakan pernyataan. Hapus dengan join
memiliki sintaks yang rumit di mana Anda harus membuat daftar nama tabel terlebih dahulu dan kemudian menentukan from
klausa dengan gabungan:
delete wineries
from wineries join
(select email, max(id) as maxid
from wineries
group by email
having count(*) > 1
) we
on we.email = wineries.email and
wineries.id < we.maxid;
Atau tulis ini sebagai exists
klausa:
delete from wineries
where exists (select 1
from (select email, max(id) as maxid
from wineries
group by email
) we
where we.email = wineries.email and wineries.id < we.maxid
)