2016-11-11 11 views
0

where句を含む1つのテーブルの内容を同じテーブルに別のwhere句にコピーする方法を教えてください。SQL INSERT from where句

したがって、選択クエリは次のようになります。 SELECT * FROM items where countryNr = 5;

上記のクエリのすべてのデータを、たとえばcountryNr = 1に挿入します。

したがってcountryNr = 5とCountryNr = 1は、同じデータの最後にhabeする必要があります。私は質問が、何かなどを理解していない

+2

の可能性のある重複(http://stackoverflow.com/questions/9156340/how-to-copy- [行をコピーして、MySQLで自動インクリメントフィールドと同じテーブルに挿入するには?]:このようなa-row-and-in-as-in-a-autoインクリメントフィールドインmysql) – raumkrieger

+0

W select(*を使用するのではなく)のすべての値を綴る必要がありません。selectのcountryNRのフィールド名の代わりに1を入れるだけですか?またはdupのhttp://stackoverflow.com/questions/5253302/insert-into-select-for-all-mysql-columnsかもしれませんか? – xQbert

答えて

1
あなたはより多くのINSERTよりUPDATEのようなものである記述したもの

...

update item 
set (field1, field2) = (select field1, field2 from item where countrNR = 5) 
where countrNR = 1 
+0

私はそれがうまくいくと思います。私は後でそれを試してみる。私はこれを前に試しましたが、私が思ったように実際には機能しませんでした。 BtnNr、ScreenNr、DepartNr、FunctionNr、TypeNr、ItemNr、BtnTextのいずれかを選択します。BtnNr、ScreenNr、DependNr、FunctionNr、BtnText、BonText、Qty、Price、MWStSatzNr、Enabled、Visible、TextColor、LastUpdate、VirtualKeyNr) 、BonText、Qty、Price、MWStSatzNr、Enabled、Visible、TextColor、LastUpdate、VirtualKeyNr FROM ts_items WHERE FilialNr = 5 –

0

...クエリが何か可能性があり、その場合のmySQL docs for insert into select

Insert into items Select Field1, Field2, Field3, Field4..., 1 as countryNR 
from items where countrNR = 5