Nå, du svarer sådan set på dit spørgsmål. Du ser ud til at have max(id)
:
SELECT email, COUNT(email) AS occurences, max(id)
FROM wineries
GROUP BY email
HAVING (COUNT(email) > 1);
Du kan slette de andre ved hjælp af erklæringen. Slet med join
har en vanskelig syntaks, hvor du først skal angive tabelnavnet og derefter angive from
klausul med joinforbindelsen:
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;
Eller skrive dette som en exists
klausul:
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
)