Gør det ved at bruge et JOIN mod et undervalg for at få det højeste id fra 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
Ikke direkte testet, da jeg ingen testdata har!
EDIT - Et par forsøg på at nå top 5, men igen ikke testet
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
Eller en anden måde.
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