0

我想用一个查询更新多行。我正在使用 MySQL。

什么是正确的查询?

这是一个示例,我需要使用一个查询更新多个姓氏。

开始:

name      last_name
______    ________
James     abcasd
Becky     hadsfd
Richard   adfdfadgg

结束:

name      last_name
______    ________
James     Jamerson
Becky     Beckerdude
Richard   O'Nerdy
4

1 回答 1

3

if are limited cases, can use Case Statement.

update yourTable set Last_Name = CASE When Name = 'James' then 'Jamerson'
                                 WHEN Name = 'Becky' then 'Beckerdude'..
                                 --then for each case
                                 END

edit

if you got the relation Name - NewLastName in another table, then create a script:

select
'When Name = '''+CAST(Name as Varchar(50)+''' Then '''+Cast(LastName as Varchar(50)+'''
from yourTableWithRelation

this will generate all When Name then LastName then add it to the update

Update yourTable set Last_Name = CASE 
--Paste here the generated
END

edit2

another BEST way to update, if you got the relation in another table:

Update T set T.Last_Name = T2.LastName from YourTableToUpdate T inner join TableWithNewLastName T2 
on T1.Name = T2.Name
于 2012-07-29T22:14:45.767 回答