salix/db/versions/.archive/10205-bronzeAnthurium/00-firstScript.sql

33 lines
1.1 KiB
SQL

DROP TEMPORARY TABLE IF EXISTS tmp.priority;
CREATE TEMPORARY TABLE tmp.priority
ENGINE = MEMORY
SELECT itemTagId, MaxPriority
FROM (SELECT it.id itemTagId, priorityChange.MaxPriority, i.id
FROM vn.itemTag it
JOIN vn.item i ON i.id = it.itemFk
JOIN vn.itemType ity ON ity.id = i.typeFk
JOIN (SELECT it.itemFk, max(it.priority) + 1 as MaxPriority
FROM vn.itemTag it
GROUP BY it.`itemFk`
) priorityChange ON priorityChange.itemFk = i.id
JOIN(SELECT it.itemFk,it.priority, min(it.id) as minId
FROM vn.itemTag it
GROUP BY it.`itemFk`,it.`priority`
) minId ON minId.itemFk = i.id
AND minId.priority = it.priority
WHERE i.isActive
GROUP BY it.itemFk, it.priority
HAVING COUNT(it.priority) > 1)sub
GROUP BY sub.id;
SET @isTriggerDisabled := TRUE;
UPDATE vn.itemTag it
JOIN tmp.priority tmp ON tmp.itemTagId = it.id
SET it.priority = tmp.MaxPriority;
SET @isTriggerDisabled := FALSE;
DROP TEMPORARY TABLE IF EXISTS tmp.priority;
ALTER TABLE `vn`.`itemTag`
ADD UNIQUE INDEX `itemTagItemPriority` (`itemFk` ASC, `priority` ASC);