使用union all在sql中连接三个表

时间:2015-07-28 04:44:21

标签: sql oracle join left-join union-all

CREATE TABLE ZIPCODE_16P2
(
ZIPCODE NUMBER (10) NOT NULL,
CITY    VARCHAR2 (50) NOT NULL,
STATE   VARCHAR2 (50) NOT NULL,
PRIMARY KEY (ZIPCODE)
);


CREATE TABLE REGISTER_LSP_16P2
(
LAB_ID               NUMBER (10) NOT NULL,
PASSWORD             VARCHAR2 (255) NOT NULL,
LAB_NAME             VARCHAR2 (255) NOT NULL,
ADDRESS              VARCHAR2 (255) NOT NULL,
ZIPCODE              NUMBER (10) NOT NULL,
CONTACT_NUMBER       NUMBER (10) NOT NULL,
ALTERNATE_CONTACT_NO NUMBER (10) NOT NULL,
EMAIL                VARCHAR2 (255) NOT NULL,
CATEGORY             VARCHAR2 (255) NOT NULL,
SECURITY_QUESTION    VARCHAR2 (255) NOT NULL,
SECURITY_ANSWER      VARCHAR2 (255) NOT NULL,
PRIMARY KEY (LAB_ID),
FOREIGN KEY (ZIPCODE) REFERENCES ZIPCODE_16P2 (ZIPCODE),
UNIQUE (PASSWORD),
UNIQUE (CONTACT_NUMBER),
UNIQUE (ALTERNATE_CONTACT_NO),
UNIQUE (EMAIL)
);



CREATE TABLE TBL_TEST_786
(
TEST_CODE        NUMBER (30),
LAB_ID           NUMBER (30),
TEST_NAME        VARCHAR2 (255),
TEST_DESCRIPTION VARCHAR2 (255),
TEST_DURATION    NUMBER (30),
COST_OF_THE_TEST NUMBER (30),
LAB_HOME         VARCHAR2 (255)
);

我有三张桌子,我想加入他们以获取必填字段LAB_NAME,ADDRESS,ZIPCODE,STATE,CITY,TEST_NAME,TEST_CODE,TEST_DESCRIPTION,COST_OF_THE_TEST,LAB_HOME。 我尝试加入内部联接,但没有得到结果,请你帮助我。

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT LAB_NAME,ADDRESS,REGISTER_LSP_16P2.ZIPCODE,STATE,CITY,TEST_NAME,TEST_CODE,TEST_DESCRIPTION,COST_OF_THE_TEST,LAB_HOME FROM
 REGISTER_LSP_16P2 join ZIPCODE_16P2 on REGISTER_LSP_16P2.ZIPCODE=ZIPCODE_16P2.ZIPCODE join TBL_TEST_786 on REGISTER_LSP_16P2.LAB_ID=TBL_TEST_786.LAB_ID