简体   繁体   English

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

[英]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.

相关问题 错误:在createSQLQuery中找不到列&#39;col_name&#39; - ERROR: Column 'col_name' not found with createSQLQuery 我应该如何处理 mysql 触发器中字段列表中的未知“Col_name”列? - What should I do with the unknown 'Col_name' column in field list in mysql trigger? mysql(5.1)插入语法&gt; col_name = value? - mysql (5.1) insert syntax > col_name=value? MySql SELECT * FROM table_name WHERE col_name IN(任何值) - MySql SELECT * FROM table_name WHERE col_name IN (any value) 是否可以将表名导出为“ NEW”。 <TABLE FEILD> “在MySQL触发器中? - Is it possible to derive table name as “NEW.<TABLE FEILD>” in MySql Trigger? mysql : 选择 where with group by with GROUP_CONCAT(col_name SEPARATOR &#39;, &#39;) - mysql : select where with group by with GROUP_CONCAT(col_name SEPARATOR ', ') 不使用“ where”子句,但出现错误:“未知列” <col_name> 在where子句中” - Not using `where` clause, but getting error:“unknown column <col_name> in where clause” 如何在postgresql中的查询中写SUM(IF(boolean,integer,integer))col_name - How to write SUM(IF(boolean, integer, integer)) col_name in query in postgresql 如何使用SET col_name =语法使mysqldump输出 - How do I make mysqldump output using the SET col_name= syntax UPDATE子句中VALUES(col_name)函数的性能 - Performance of VALUES(col_name) function in the UPDATE clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM