我必须使用 SET 列名作为从另一个表中选择的变量来更新表。 这是我的 MySQL :
UPDATE famcharts_old.db1w_matrix
JOIN famcharts_old.tbl_dirtosv
ON famcharts_old.tbl_dirtosv.PlaceOT = famcharts_old.db1w_matrix.SERVICE_ID
and famcharts_old.tbl_dirtosv.Dir_ID IN (a LIST-of-values)
JOIN famcharts_old.tbl__dir__base
ON all_directives.DirID = famcharts_old.tbl_dirtosv.Dir_ID
SET famcharts_old.db1w_matrix.**@variable** = famcharts_old.tbl_dirtosv.NiceNeed;
@variable 来自选择,其中变量等于值列表中的 Id。
我尝试了几次 JOINS,但无法将 @variable 放入列的名称中。
尝试将此更新命令与准备好的语句一起使用
set @variable = 'column_name';
set @cmdSql = "UPDATE famcharts_old.db1w_matrix
JOIN famcharts_old.tbl_dirtosv ON famcharts_old.tbl_dirtosv.PlaceOT =
famcharts_old.db1w_matrix.SERVICE_ID
and famcharts_old.tbl_dirtosv.Dir_ID IN (a LIST-of-values)
JOIN famcharts_old.tbl__dir__base
ON all_directives.DirID = famcharts_old.tbl_dirtosv.Dir_ID
SET famcharts_old.db1w_matrix.";
set @cmdSql=Concat(@cmdSql,@variable);
set @cmdSql=Concat(@cmdSql," = famcharts_old.tbl_dirtosv.NiceNeed; ");
Prepare execSQL from @cmdSql;
Execute execSQL;
DEALLOCATE PREPARE execSQL;
更多说明查看:https://dev.mysql.com/doc/refman/8.4/en/sql-prepared-statements.html