如何将一些行复制到mysql中的其他行

时间:2011-03-28 17:51:15

标签: mysql

有人可以说我怎样才能复制这样的行:

将字段名称从id 1-10复制到id为31-40

示例:

1 foo
...
31 bla

之后应该是:

1 foo
...
31 foo

3 个答案:

答案 0 :(得分:0)

UPDATE table A
set name = (select B.name from table B where B.Id = A.Id - 30)
where A.id between 31 AND 40

答案 1 :(得分:0)

像这样......

UPDATE tablename    
SET fieldname = ( SELECT tablename.fieldname
FROM tablename t2
WHERE t2.id = (tablename.id-30))

WHERE tablename.id BETWEEN 31 AND 40;

答案 2 :(得分:0)

UPDATE TABLE A INNER JOIN TABLE B ON A.Id = B.Id + 30
SET A.NAME = B.NAME
WHERE A.Id Between 31 and 40