gpt4 book ai didi

mysql - 如何从mysql中获取最后修改的列名

转载 作者:行者123 更新时间:2023-11-30 23:05:09 25 4
gpt4 key购买 nike

是否可以从记录中获取特定的更新列名?

如果是这样,请帮助我。因为我正在创建一个历史表来存储更新的列及其来自其余表的值。

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;

我已经创建了触发器,但正在搜索上次更新的列名称的子查询。提前致谢。

最佳答案

试试这个代码...

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;

分隔符///

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;

///

分隔符;

关于mysql - 如何从mysql中获取最后修改的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com