MySQL UPDATE on first column
I have many tables which their ID columns' name is different but column name subjected to update stays same.
For exam开发者_JAVA百科ple
Columns:
fooID | x | y | UPDATEME | t | ..
barID | a | UPDATEME | b | ..
bazID | UPDATEME | m | l | ..
As you can see, the ID column is always first but column names changing. And I want to update UPDATEME
column regardless the ID columns name.
UPDATE `UPDATEME` WHERE `FIRST_COLUMN` = #
How can I accomplish this? I am aware of ORDER BY column_order
syntax but I can't use this in WHERE
clause.
There is no SQL syntax to do that. But you can access DB metadata (INFORMATION_SCHEMA) to generate your update sentences.
Something like this:
select
concat(
'update ',
table_name,
' set updateme=1212 where ',
column_name,
'=12123;'
)
from information_schema.columns
where ordinal_position = 1;
精彩评论