【发布时间】:2021-06-16 18:42:00
【问题描述】:
我正在尝试插入来自同一个表的值 id_team_FK 和 id_location_FK。 例如 id_team_FK = 4 和 id_location_FK = 150。问题是,id_team_FK 必须是从 team_table 中随机选择的值(这可行),我需要知道确切的数字(例如 4)才能实现分配给它的 id_location_FK(对于例 150)。如何让它发挥作用?
insert into test (PK, id_team_FK,id_location_FK)
values (1,
-- Selects random number from table
(select * from (id_team_FK from team_table order by dbms_random.random) where rownum=1),
-- needs to know the value of id_team_FK to know what select...
(select * from ( select id_location_FK from team_table) where team_table.id_team_FK = id_team_FK));
【问题讨论】:
-
如果您从同一个表中选择两列,则只需使用一个
select选择它们。id_team_FK和id_location_FK之间是否存在一对多的关系? -
@astentx 这是一对一的关系
-
那为什么不
insert into test select * from (select 1 as id, id_team_fk, id_lication_fk from team_table order by dbms_random.random()) where rownum = 1)? -
@astentx 也可以,谢谢!
标签: sql oracle oracle-sqldeveloper sqlplus