mysql:使用已有的记录更新另一条数据

create table test(

    id integer primary key,

   name varchar(100)

);


例如现在在test表中有N条数据,其中有两条为

12, 'hello'

13, 'world'

如果想把上一条中的数据改成和下一条记录一样(id除外),那么当数据比较少时,可以直接使用

update test set name='world' where id=12;

但如果这个表中的数据比较多时,这么做就有些麻烦了,可以利用join的方式:

update test a join test b on a.id=12 and b.id=13 set a.name = b.name;

注意:是join,而不是left join或 right join   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章