简体   繁体   中英

How select from two different tables with multiple foreign keys

First explain what I need using an image:

在此处输入图像描述

Second creation tables:

CREATE SEQUENCE  SEQ_tab7  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 2206 NOCACHE  ORDER  NOCYCLE  NOKEEP  NOSCALE  GLOBAL ;
CREATE SEQUENCE  SEQ_tab8  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 2206 NOCACHE  ORDER  NOCYCLE  NOKEEP  NOSCALE  GLOBAL ;

CREATE TABLE TABLE_7 
(
     ID NUMBER(19,0) DEFAULT SEQ_tab7.nextval  NOT NULL
    ,DESCRIPTION    VARCHAR2(256) NOT NULL
    ,CONSTRAINT TAB_7_PK PRIMARY KEY (ID) ENABLE
);

CREATE TABLE TABLE_8 
(
     ID NUMBER(19,0) DEFAULT SEQ_tab8.nextval  NOT NULL
    ,FIELD_1    NUMBER(19,0) NOT NULL
    ,FIELD_2    NUMBER(19,0) NOT NULL
    ,FIELD_3    NUMBER(19,0) NOT NULL
    ,FIELD_4    VARCHAR2(256)
    ,CONSTRAINT TAB_8_PK PRIMARY KEY (ID) ENABLE
    ,CONSTRAINT tab_8_FIELD_1_FK FOREIGN KEY (FIELD_1) REFERENCES TABLE_7(ID) 
    ,CONSTRAINT tab_8_FIELD_2_FK FOREIGN KEY (FIELD_2) REFERENCES TABLE_7(ID) 
    ,CONSTRAINT tab_8_FIELD_3_FK FOREIGN KEY (FIELD_3) REFERENCES TABLE_7(ID) 
);

Third - inserts:

Insert into TABLE_7 (ID,DESCRIPTION) values ('1','desc_1');
Insert into TABLE_7 (ID,DESCRIPTION) values ('2','desc_2');
Insert into TABLE_7 (ID,DESCRIPTION) values ('3','desc_3');


Insert into TABLE_8 (ID,FIELD_1,FIELD_2,FIELD_3,FIELD_4) values ('23','1','2','1','lorem_1');
Insert into TABLE_8 (ID,FIELD_1,FIELD_2,FIELD_3,FIELD_4) values ('43','1','3','3','lorem_2');
Insert into TABLE_8 (ID,FIELD_1,FIELD_2,FIELD_3,FIELD_4) values ('54','3','3','3','lorem_3');

How could I get the desired results?

Best regards

Use multiple joins

Try this:

SELECT Base.ID, FirstJoin.Desc, SecondJoin.Desc, ThirdJoin.Desc, Base.field_4
FROM table_2 Base
JOIN table_1 FirstJoin ON FirstJoin.ID = Base.Field_1
JOIN table_1 SecondJoin ON SecondJoin.ID = Base.Field_2
JOIN table_1 ThirdJoin ON ThirdJoin.ID = Base.Field_3

Here is an example which use subqueries for each field_? it will search in table_1 for the description and use it as an attribute. I added alias for the field to make the output more clear.

SELECT 
  table_2.id, 
  (SELECT table_1.desc FROM table_1 WHERE table_1.id=table_2.field_1) as "FIELD_1",
  (SELECT table_1.desc FROM table_1 WHERE table_1.id=table_2.field_2) as "FIELD_2",
  (SELECT table_1.desc FROM table_1 WHERE table_1.id=table_2.field_3) as "FIELD_3",
  table_2.field_4
FROM table_2;

For example the first row of table_2 is (id=23, field_1=1, field_2=2, field_3=1, field_4="lorem_1") The second attribute after performing the suggested query will be the result of the query SELECT table_1.desc FROM table_1 WHERE table_1.id=table_2.field_1 which is "desc_1" and the same process for the field_2 and field_3 in that case the result for the first row will be:

ID  FIELD_1 FIELD_2 FIELD_3 FIELD_4
23  desc_1  desc_2  desc_1  lorem_1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM