![](/img/trans.png)
[英]How to insert into table with values from another tables - mysql inner join
[英]how to Insert table from another tables
我有两个表art和art_tag_art。 这是艺术栏表:
id
title
slug
image
这是列art_tag_art
id
art_id
tag_id
我想将所有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
试试这个:
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;
您可以这样做,但看起来并不那么明智
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.