如何复制一行并插入同一个表与自动增量字段在MySQL? [英] How to copy a row and insert in same table with a autoincrement field in MySQL?

查看:339
本文介绍了如何复制一行并插入同一个表与自动增量字段在MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格。我想做的是复制一个自动增量列ID = 1的行,并将数据插入到行和列ID = 2的同一个表中。

I have a table Eg- tab . What I am trying to do is copying a row with an autoincrement column ID=1 and insert the data into same table with a row and column ID=2.

使用MySql 。
如何在单个查询中执行此操作请帮助

Using MySql. How can I do this in a single query?Please help

推荐答案

使用 INSERT。 .. SELECT

insert into your_table (c1, c2, ...)
select c1, c2, ...
from your_table
where id = 1

其中 c1,c2,... 都是除 id 之外的所有列。如果你想显式插入一个 id 2,然后在你的INSERT列表和你的SELECT:

where c1, c2, ... are all the columns except id. If you want to explicitly insert with an id of 2 then include that in your INSERT column list and your SELECT:

insert into your_table (id, c1, c2, ...)
select 2, c1, c2, ...
from your_table
where id = 1

您必须处理可能的重复 / code> of 2在第二种情况下。

You'll have to take care of a possible duplicate id of 2 in the second case of course.

这篇关于如何复制一行并插入同一个表与自动增量字段在MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆