更新列名称和值等于给定值

时间:2017-06-13 09:29:07

标签: mysql sql

我需要更新列名和列值等于给定数据的所有表中的值

SELECT * FROM(
  SELECT table_schema, table_name  AS table_name_str 
   FROM information_schema.columns 
   WHERE column_name = 'customerID' AND table_schema='testdb' 
) AS tb1
WHERE table_name_str.customerID=5

当我尝试运行它时,它会显示以下错误消息:

  

[Err] 1054 - 'where'中的未知列'table_name_str.customerID'   条款“

2 个答案:

答案 0 :(得分:0)

SELECT * FROM(
  SELECT table_schema, table_name  AS table_name_str ,customerID
   FROM information_schema.columns 
   WHERE column_name = 'customerID' AND table_schema='testdb' 
) AS tb1
WHERE tb1.customerID=5

尝试以上代码。

你必须在内部查询中指定该列,然后只能在外部查询中放置条件。

希望这有帮助。

答案 1 :(得分:0)

这可以通过两个步骤完成:

 1.创建UPDATE查询字符串:

SELECT concat('UPDATE ', table_schema,'.', table_name,' SET customerID=<new_value> WHERE customerID=5;' as qry_str
FROM(
  SELECT table_schema, table_name  
  FROM 
    information_schema.columns 
  WHERE 
    column_name = 'customerID' 
    AND table_schema='testdb'
) AS tb1

2。运行UPDATE语句:

UPDATE testdb.table1 SET customerID=6 where customerID=5;
UPDATE testdb.table5 SET customerID=6 where customerID=5;
...
...
and so on.

让我知道这是否有效。