是否可以从记录中获取特定的更新列名?
如果是的话,请帮助我。因为我正在创建一个历史表来存储更新的列及其余表中的值。
DROP TRIGGER IF EXISTS testing.hisupdate;
CREATE TRIGGER testing.hisupdate AFTER
UPDATE ON testing.emp
FOR EACH ROW
INSERT INTO testing.history (`id`,`revision_id`,`trx_code`, `trx_method`, `column_name`, `old_value`, `new_value`, `trx_timestamp`, `user_id`)
SELECT
(SELECT emp_id
FROM testing.emp
ORDER BY last_update DESC LIMIT 1),
(SELECT p.revision_id
FROM testing.history AS p
WHERE p.id=
(SELECT emp_id
FROM testing.emp
ORDER BY last_update DESC LIMIT 1)
ORDER BY trx_timestamp DESC LIMIT 1) + 1, 'updt', 'update','null',
(SELECT p.new_value
FROM testing.history AS p
WHERE p.id=
(SELECT emp_id
FROM testing.emp
ORDER BY last_update DESC LIMIT 1)
ORDER BY trx_timestamp DESC LIMIT 1), concat(p.emp_id,"/",p.emp_name,"/",p.salary,"/") AS new_value,
NOW(),
'101'
FROM testing.emp AS p
WHERE p.emp_id = new.emp_id;
我创建了触发器但是搜索了最后更新列名的子查询。 提前谢谢。
答案 0 :(得分:0)
试试此代码......
create table sales(orderno INT, sale INT,empsalary int, ts TIMESTAMP);
create table history(updated varchar(20), oldvalue INT,newvalue INT);
INSERT INTO sales (orderno,sale,empsalary) VALUES(1,700,7000);
INSERT INTO sales (orderno,sale,empsalary) VALUES(2,800,8000);
INSERT INTO sales (orderno,sale,empsalary) VALUES(3,900,9000);
DROP TRIGGER test.instrigger;
DELIMITER ///
CREATE TRIGGER test.instrigger AFTER UPDATE ON sales
FOR EACH ROW
BEGIN
IF NEW.sale <> OLD.sale THEN
INSERT INTO history (updated, oldvalue, newvalue) VALUES('sale', OLD.sale,NEW.sale);
END IF;
IF NEW.empsalary <> OLD.empsalary THEN
INSERT INTO history (updated, oldvalue, newvalue) VALUES('empsalary', OLD.empsalary,NEW.empsalary);
END IF;
END;
///
DELIMITER;