如何从其他表插入表格
我有两个表格,art和art_tag_art。 这是列艺术表:如何从其他表插入表格
id
title
slug
image
这列art_tag_art
id
art_id
tag_id
我想复制所有的艺术。 id到art_tag_art.art_id并同时复制所有内容新行中的art_tag_art.tag_id(已存在)。
我曾尝试和搜索几个MySQL的句法插入和选择:
select id into @aid from art;
select tag_id into @tid from art_tag_art;
insert into art_tag_art (art_id, tag_id) values (@aid,@tid);
INSERT INTO art_tag_art (art_id, tag_id)
SELECT art.id, art_tag_art.tag_id
FROM art, art_tag_art order by id;
但没有任何工程。这一次,我得到一个错误信息:Error Code: 1364. Field 'id' doesn't have a default value
试试这个:
select [email protected] from art order by id; select [email protected] from art_tag_art; insert into art_tag_art (art_id, tag_id) values (@aid,@tid);
INSERT INTO art_tag_art (art_id, tag_id) SELECT art.id, art_tag_art.tag_id FROM art, art_tag_art order by id;
谢谢先生,我试过了,但在新行中的值tag_id并不是我所期望的。 –
你能做到这一点,但它不举目皆是明智的
insert into art_tag_art(art_id,tag_id)
select a.id ,(select at.tag_id from art_tag_art at where at.id = a.id - 25)
from art a;
谢谢先生。我会试一试 –
你的Id是一个自动增量字段吗? –
@AmeyaDeshpande,是的。 –
你可以把一些虚拟数据给它。所以很容易理解你正在尝试做什么 –