2017-01-16 133 views
0

我有两个表格,art和art_tag_art。 这是列艺术表:如何从其他表插入表格

id 
title 
slug 
image 

这个数值艺术表 enter image description here

这列art_tag_art

id 
art_id 
tag_id 

值art_tag_art enter image description here

我想复制所有的艺术。 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

期望值: enter image description here

+1

你的Id是一个自动增量字段吗? –

+0

@AmeyaDeshpande,是的。 –

+0

你可以把一些虚拟数据给它。所以很容易理解你正在尝试做什么 –

回答

2

试试这个:

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;

+0

谢谢先生,我试过了,但在新行中的值tag_id并不是我所期望的。 –

0

你能做到这一点,但它不举目皆是明智的

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; 
+0

谢谢先生。我会试一试 –