私は2つのテーブルtable1
とtable2
を持っています。これらの表には、一意のname
とid
の列があります。Oracle INSERTステートメントと異なるテーブルでの選択
また、関係/結合テーブルtable1_table2
は、ストレートフォワード列table1_id
とtable2_id
を持っています。私が何をしたいか
は私がのために関係を作成したいtable1
とtable2
の要素のname
のを知っtable1_table2
に新しい関係を挿入することです。しかし、id
をtable_table2
に挿入する必要があります。私もまた、うまくいきませんでした
insert into table1_table2 values ((select id from (select id from table1 where name = 'some_name') where rownum=1), (select id from (select id from table2 where name = 'another_name') where rownum=1))
を使用してみました
insert into table1_table2 values ((select id from table1 where name = 'some_name'), (select id from table2 where name = 'another_name'))
:私が欲しいもの
はそのようなものです。
私は最初にid
を抽出することができますが、必要に応じて1つのステートメントに含めることをお勧めします。
編集:
table1
id name
1 foo
2 bar
table2
id name
1 some
2 data
table1_table2
table1.id table2.id
1 1
、今私は
table1.id table2.id
2 2
を挿入したい:私もまた
例のデータを動作しませんでした
insert into table1_table2 values (select t1.id, t2.id from table1 t1, table2 t2 where t1.name = 'some_name' and t2.name = 'another_name')
を試してみました
をtable1_table2
に変換するが、I d table1
のエントリがname
bar
であり、table2
のエントリがname
data
であることがわかっているだけです。
いくつかのサンプルデータと、テーブル構造のより明確な見方が役立ちます。名前が一致する各テーブルのIDを指定しますか?名前が固定されているので、すべての名前または特定の名前のIDが必要ですか?名前が1つのテーブルにのみ表示される場合はどうなりますか? –
「うまくいかない」とは何かを説明してください。 –