mysql field name from variable

前端 未结 3 893
别那么骄傲
别那么骄傲 2020-11-28 13:17

is it possible to select field which name is string?

SELECT \'fieldname\' FROM table

i need this for trigger to have dynamic field names somethi

3条回答
  •  再見小時候
    2020-11-28 14:01

    If you want to select more than one column:

    SELECT GROUP_CONCAT(COLUMN_NAME) FROM information_schema.`COLUMNS` C 
    WHERE table_name = 'MyTb' AND COLUMN_NAME LIKE '%whatever%' INTO @COLUMNS;
    
    SET @table = 'MyTb';
    SET @s = CONCAT('SELECT ',@columns,' FROM ', @table);
    
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    

提交回复
热议问题