[英]How to get a column (NEW.{{col_name}}) if its name is stored in a variable - MySQL
I have a variable: 我有一个变量:
DECLARE col_name CHAR(255);
How can I take column value from NEW/OLD
if its name is stored in a variable. 如果名称存储在变量中,如何从
NEW/OLD
获取列值。
NEW.{{col_name}}
OLD.{{col_name}}
I need to use this in the trigger. 我需要在触发器中使用它。
MySql 5.5.40 MySQL的5.5.40
if there is known scope of possible values (and i belive there is) one can use 'case' 如果存在可能值的已知范围(我相信存在),则可以使用“ 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
...
If type of column differs - then cast them to text.. 如果列的类型不同,则将其转换为文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.