2017-01-16 15 views
0

私は2つのtable、a​​rt、art_tag_artを持っています。 これは、列の芸術テーブルです:別のテーブルからテーブルを挿入する方法

id 
title 
slug 
image 

この値アートテーブル enter image description here

これは私がすべてのアートをコピーしたい enter image description here

art_tag_art

id 
art_id 
tag_id 

値列art_tag_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

+1

あなたのIDは自動インクリメントフィールドですか? –

+0

@AmeyaDeshpande、そうです。 –

+0

ダミーのデータを入れてもいいですか?だからあなたが何をしようとしているのかを理解するのは簡単でしょう –

答えて

2

はこの1つを試してみてください:

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

ありがとうございます。私はそれを試してみましょう –

関連する問題