sql查询将城市浦那替换为孟买和孟买以填补表格

时间:2014-10-31 11:28:12

标签: mysql sql

我有一个表名为emp的列名

city                         output table
-----
mumbai                           pune
pune                             mumbai
mumbai                           pune
pune                             mumbai
pune                             mumbai

我希望更新查询将pune替换为mumbai和mumbai to pune

2 个答案:

答案 0 :(得分:1)

您可以使用查询执行此操作:

select (case when city = 'mumbai' then 'pune'
             when city = 'pune' then 'mumbai'
             else city
        end)
. . .

如果您想更改值,请:

update table t
     set city = (case when city = 'mumbai' then 'pune'
                      when city = 'pune' then 'mumbai'
                 end)
     where city in ('mumbai', 'pune');

答案 1 :(得分:1)

尝试以下方法:

UPDATE TABLENAME
SET city= (
CASE 

WHEN city = 'mumbai'
THEN 'pune'

WHEN city = 'pune'
THEN 'mumbai'

END )

希望它有所帮助。