如何从其他表

时间:2017-01-16 13:27:38

标签: mysql

我有两张桌子,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

我想将所有art.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

2 个答案:

答案 0 :(得分:2)

试试这个:

select id=@aid from art order by id; select tag_id=@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;

答案 1 :(得分: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;