gpt4 book ai didi

mysql - 根据另一个表上的映射值更新 a 上的 MySQL

转载 作者:行者123 更新时间:2023-11-29 09:37:56 26 4
gpt4 key购买 nike

我有一个 SQL 表,其中有一列包含非标准化值,例如:

City: 'City 1', 'City one', 'City 2/ some text'....

我有正确的值映射:

'City 1':'City 1' ,
'City one':'City 1',
'City 3/ some text':'City 3'

列表大约有 100 个案例,在 python 中我只会使用字典,但我不知道如何在 SQL 中执行此操作

我找到了以下示例:https://www.sqlservercentral.com/forums/topic/replacing-a-case-statement-in-an-update-with-table-driven-logic

并尝试遵循它:

UPDATE dbo.target_table
set city =

case

when city = 'City 1' then 'City 1'
when city = 'City one' then 'City 1'

END

但最终城市列为空。

最佳答案

您需要一个其他:

update dbo.target_table
set city = (case when city = 'City 1' then 'City 1'
when city = 'City one' then 'City 1'
else city
end)

或者,更好的是,where 子句:

update dbo.target_table
set city = (case when city = 'City 1' then 'City 1'
when city = 'City one' then 'City 1'
else city
end)
where city in ('City 1', 'City one');

关于mysql - 根据另一个表上的映射值更新 a 上的 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57211000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com