日期:2014-05-16 浏览次数:20800 次
DELIMITER $$ DROP TRIGGER /*!50032 IF EXISTS */ `tbl_afterinsert_trigger`$$ CREATE TRIGGER `tbl_afterinsert_trigger` AFTER INSERT ON `tbl` FOR EACH ROW BEGIN IF NEW.group_id > 0 THEN SET @cnt = (SELECT count(*) FROM tbl WHERE is_show = 1 AND group_id = NEW.group_id AND toprank_id = NEW.toprank_id AND time_windows = NEW.time_windows); IF @cnt > 1 THEN SELECT id INTO @id FROM tbl WHERE is_show = 1 AND group_id = NEW.group_id AND toprank_id = NEW.toprank_id AND time_windows = NEW.time_windows ORDER BY created_at DESC LIMIT 1; UPDATE tbl SET is_show = 0 WHERE group_id = NEW.group_id AND toprank_id = NEW.toprank_id AND time_windows = NEW.time_windows AND id <> @id; END IF; END IF; END; $$ DELIMITER ;