Beh, in un certo senso rispondi alla tua domanda. Sembra che tu voglia max(id)
:
SELECT email, COUNT(email) AS occurences, max(id)
FROM wineries
GROUP BY email
HAVING (COUNT(email) > 1);
È possibile eliminare gli altri utilizzando l'istruzione. Elimina con join
ha una sintassi complicata in cui devi prima elencare il nome della tabella e quindi specificare il from
clausola con il join:
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;
Oppure scrivendolo come exists
clausola:
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
)