Правете го с помощта на JOIN срещу подселекция, за да получите най-високия идентификатор от my_rel_table
DELETE my_rel_table, my_photo_table
FROM my_rel_table
INNER JOIN
(
SELECT MAX(id) AS MaxId
FROM my_rel_table
WHERE relid = 1
AND type = 1
) Sub1
ON my_rel_table.id = Sub1.MaxId
LEFT OUTER JOIN my_photo_table ON my_photo_table.typeid = my_rel_table.typeid
WHERE my_rel_table.relid = 1
AND my_rel_table.type = 1
Не е директно тестван, тъй като нямам тестови данни!
РЕДАКТИРАНЕ - Няколко опита да направите топ 5, но отново не са тествани
DELETE my_rel_table, my_photo_table
FROM my_rel_table
INNER JOIN
(
SELECT id
FROM my_rel_table
WHERE relid = 1
AND type = 1
ORDER BY id DESC
LIMIT 5
) Sub1
ON my_rel_table.id = Sub1.id
LEFT OUTER JOIN my_photo_table ON my_photo_table.typeid = my_rel_table.typeid
WHERE my_rel_table.relid = 1
AND my_rel_table.type = 1
Или по различен начин.
DELETE my_rel_table, my_photo_table
FROM my_rel_table
INNER JOIN
(
SELECT id, @Counter:[email protected]+1 AS ItemCounter
FROM my_rel_table
CROSS JOIN (SELECT @Counter:=0) Sub1
WHERE relid = 1
AND type = 1
ORDER BY id DESC
) Sub1
ON my_rel_table.id = Sub1.id
AND Sub1.ItemCounter <= 5
LEFT OUTER JOIN my_photo_table ON my_photo_table.typeid = my_rel_table.typeid
WHERE my_rel_table.relid = 1
AND my_rel_table.type = 1