Bueno, respondes a tu pregunta. Parece que quieres max(id)
:
SELECT email, COUNT(email) AS occurences, max(id)
FROM wineries
GROUP BY email
HAVING (COUNT(email) > 1);
Puede eliminar los demás usando la declaración. Eliminar con join
tiene una sintaxis complicada en la que primero debe enumerar el nombre de la tabla y luego especificar el from
cláusula con la unión:
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;
O escribiendo esto como exists
cláusula:
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
)