Mysql 更新除特定列之外的查询
目前我有这样一张桌子:Mysql 更新除特定列之外的查询,mysql,Mysql,目前我有这样一张桌子: +---------+---------+---------+---------+---------+ | column1 | column2 | column3 | column4 | column5 | +---------+---------+---------+---------+---------+ | n.v.t. | n.v.t. | n.v.t. | n.v.t. | n.v.t. | +---------+---------+---------
+---------+---------+---------+---------+---------+
| column1 | column2 | column3 | column4 | column5 |
+---------+---------+---------+---------+---------+
| n.v.t. | n.v.t. | n.v.t. | n.v.t. | n.v.t. |
+---------+---------+---------+---------+---------+
| n.v.t. | value | n.v.t. | value | n.v.t. |
+---------+---------+---------+---------+---------+
| n.v.t. | n.v.t. | n.v.t. | n.v.t. | n.v.t. |
+---------+---------+---------+---------+---------+
显然,这是一个简短的版本。目前,我需要一个查询来替换除第1列和第2列之外的所有列中的所有“n.v.t.”。我所做的就是用标准的更新查询来实现这一点。(设置第2列='replace_value'其中)等
我有没有办法把这个缩短一点?要替换除特定列以外的所有列中的所有“n.v.t.”,您可以按以下方式执行:
update table t
set col3 = (case when col3 = 'n.v.t' then 'newvalue' else col3 end),
col4 = (case when col4 = 'n.v.t' then 'newvalue' else col4 end),
. . .
coln = (case when coln = 'n.v.t' then 'newvalue' else coln end)
也就是说,如果合适,使用替换值。否则,将列设置为自身。您可以通过编程方式从mydb.mytable中通过
SHOW columns获取列
然后,在这些列上循环(不希望的列除外)以构建更新查询