Oracle SQL 在引用 2 FK 时将多行插入 table

Oracle SQL Inserting multiple rows into a table while referencing 2 FK

我正在尝试插入来自同一个 table 的值 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));

CTE 怎么样? Select ID_TEAM_FK 首先,用它来获取 ID_LOCATION_FK,插入结果。

INSERT INTO test (pk, id_team_fk, id_location_fk)
   WITH
      temp
      AS
      -- select random number from table
         (SELECT id_team_fk
            FROM (  SELECT id_team_FK
                      FROM team_table
                  ORDER BY DBMS_RANDOM.random)
           WHERE ROWNUM = 1),
      temp2
      AS
      -- now you know ID_TEAM_FK (you fetched it in the TEMP CTE), so - fetch ID_LOCATION_FK
         (SELECT id_team_fk, id_location_fk
            FROM (SELECT a.id_team_fk, b.id_location_FK
                    FROM team_table a CROSS JOIN temp b
                   WHERE a.id_team_FK = b.id_team_FK))
   SELECT 1, id_team_fk, id_location_fk
     FROM temp2;