【发布时间】:2017-08-23 03:41:14
【问题描述】:
我有两张桌子table1 和table2。这些表具有唯一的 name 和 id 列。
我还有一个关系/连接表table1_table2,其中包含table1_id 和table2_id 的直接列。
我想要做的是在知道table1 和table2 中的元素的names 中插入一个新关系到table1_table2 我想为其创建一个关系。但我需要让他们的ids 将它们插入table_table2。
我想要的是这样的:
insert into table1_table2 values ((select id from table1 where name = 'some_name'), (select id from table2 where name = 'another_name'))
我也尝试过使用
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))
这也没用。
我知道如有必要,我可以先提取 ids,但我希望它出现在一个语句中。
编辑:我也试过了
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
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
进入table1_table2,但我只知道table1 中的条目有name bar,table2 中的条目有name data。
【问题讨论】:
-
一些示例数据和更清晰的表结构视图可能会有所帮助。您是否想要名称匹配的每个表中的 ID - 对于所有名称或特定名称,因为您已将
'name'显示为固定?如果一个名字只出现在一个或另一个表中怎么办? -
请解释“不起作用”是什么意思。
标签: sql oracle plsql relational-database oracle-xe