No, odpovídáš na svou otázku. Zdá se, že chcete max(id)
:
SELECT email, COUNT(email) AS occurences, max(id)
FROM wineries
GROUP BY email
HAVING (COUNT(email) > 1);
Ostatní můžete smazat pomocí příkazu. Smazat pomocí join
má složitou syntaxi, kde musíte nejprve uvést název tabulky a poté zadat from
klauzule se spojením:
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;
Nebo to napište jako exists
klauzule:
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
)