我的nav_item
表格如下:
+----+--------+--------+--------+------+-----+
| id | nav_id | active | name | link | ... |
+----+--------+--------+--------+------+-----+
| 1 | 1 | 1 | Item 1 | .......... |
| 2 | 2 | 1 | Item 2 | .......... |
| 3 | 1 | 1 | Item 3 | .......... |
| 4 | 3 | 1 | Item 4 | .......... |
| 5 | 1 | 1 | Item 5 | .......... |
+----+--------+--------+--------+------+-----+
我的目标是复制所有nav_id = 1
的记录。所以这个命令将起到魔力:insert into nav_item select * from nav_item where nav_id=1;
但我不希望所有人都nav_id
相同。因此,所有新克隆都应该nav_id
,例如4
。但是,如何复制所有这些内容并使用单个命令将nav_id
更改为我想要的任何内容?
答案 0 :(得分:1)
您可以使用以下查询:
INSERT INTO nav_item
SELECT NULL,
4,
active,
name,
link,
...
FROM nav_item
WHERE nav_id = 1;
答案 1 :(得分:1)
试试这个;)
insert into nav_item
select (t.id, t.nav_id, t.active, t.name, t.link, ...)
from (
select nav_item.id, @nav_id := @nav_id + 1 as nav_id, nav_item.active, nav_item.name, nav_item.link, ...information_schema.
from nav_item join (select @nav_id := (select max(nav_id) from nav_item) tmp) where nav_item.nav_id = 1
) t