gpt4 book ai didi

mysql - 如果列的名称存储在变量中,如何获取列 (NEW.{{col_name}}) - MySQL

转载 作者:行者123 更新时间:2023-11-29 17:56:09 25 4
gpt4 key购买 nike

我有一个变量:

DECLARE col_name CHAR(255);

如果其名称存储在变量中,如何从 NEW/OLD 中获取列值。

NEW.{{col_name}}
OLD.{{col_name}}

我需要在触发器中使用它。

MySql 5.5.40

最佳答案

如果存在已知的可能值范围(并且我相信存在)可以使用“case”

SELECT
CASE
when @col_name = 'columnA' then NEW.columnA
when @col_name = 'columnB' then NEW.columnB
-- and so on..
else 'unknown column'
END AS [NEW_col_value],
CASE
when @col_name = 'columnA' then OLD.columnA
when @col_name = 'columnB' then OLD.columnB
-- and so on..
else 'unknown column'
END AS [OLD_col_value]
FROM
...

如果列的类型不同 - 则将它们转换为文本..

关于mysql - 如果列的名称存储在变量中,如何获取列 (NEW.{{col_name}}) - MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804245/

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