Answer the question
In order to leave comments, you need to log in
The delete query is not executed, although the same select query is being executed, can you help why?
there is an mdate table, where there are 3 columns user_id, message_id, message_date, and I need to delete all messages except the last ones from each user.
select * from mdate m1
LEFT JOIN (
SELECT _user_id, max(message_date) as 'max_message_date'
FROM mdate GROUP BY _user_id ) m2
ON m1.message_date = m2.max_message_date AND
m1._user_id = m2._user_id
WHERE m2._user_id is null;
delete from mdate m1
LEFT JOIN (SELECT _user_id, max(message_date) as 'max_message_date'
FROM mdate GROUP BY _user_id ) m2 ON m1.message_date = m2.max_message_date
AND m1._user_id=m2._user_id
WHERE m2._user_id is null;
delete from mdate m0 where m0.message_id not in
(select m1.message_id from mdate m1
JOIN ( SELECT mg._user_id, max(mg.message_date) as 'max_message_date'
FROM mdate as mg GROUP BY mg._user_id ) m2
ON m1.message_date = m2.max_message_date
AND m1._user_id = m2._user_id);
Answer the question
In order to leave comments, you need to log in
So in the last request, remove the m0 alias, it is completely useless there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question