我有3张表格如下:
CREATE TABLE USER_STATUS ("UID" varchar2(7), "STAT_ID" varchar2(11)) ;
INSERT ALL
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_001', 'STAT_ID_001')
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_001', NULL)
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_001', NULL)
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_002', 'STAT_ID_002')
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_002', NULL)
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_003', 'STAT_ID_003')
SELECT * FROM dual;
CREATE TABLE STATUS_LKUP ("LKUP_ID" varchar2(11), "STAT_CODE" varchar2(11), "STAT_ID" varchar2(11), "STATUS" varchar2(20));
INSERT ALL
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_001', 'ST_CODE_001', 'STAT_ID_001', 'Processing')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_002', 'ST_CODE_002', 'STAT_ID_001', 'Processing')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_003', 'ST_CODE_003', 'STAT_ID_001', 'Processing')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_004', 'ST_CODE_004', 'STAT_ID_001', 'Processing')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_005', 'ST_CODE_011', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_006', 'ST_CODE_012', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_007', 'ST_CODE_013', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_008', 'ST_CODE_014', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_009', 'ST_CODE_015', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_010', 'ST_CODE_021', 'STAT_ID_001', 'Done')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_011', 'ST_CODE_022', 'STAT_ID_001', 'Done')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_012', 'ST_CODE_031', 'STAT_ID_001', 'Started')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_013', 'ST_CODE_032', 'STAT_ID_001', 'Started')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_014', 'ST_CODE_002', 'STAT_ID_002', 'Processing (Sent)')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_015', 'ST_CODE_004', 'STAT_ID_002', 'Processing (Waiting)')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_016', 'ST_CODE_014', 'STAT_ID_002', 'Issue in Prod')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_017', 'ST_CODE_012', 'STAT_ID_002', 'Issue in Prod')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_018', 'ST_CODE_021', 'STAT_ID_002', 'Done')
SELECT * FROM dual;
CREATE TABLE CORE ("CORE_ID" varchar2(11), "STAT_CODE" varchar2(11));
INSERT ALL
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_001', 'ST_CODE_001')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_002', 'ST_CODE_012')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_003', 'ST_CODE_021')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_004', 'ST_CODE_012')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_005', 'ST_CODE_012')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_006', 'ST_CODE_021')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_007', 'ST_CODE_001')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_008', 'ST_CODE_003')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_009', 'ST_CODE_012')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_010', 'ST_CODE_021')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_011', 'ST_CODE_001')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_013', 'ST_CODE_004')
SELECT * FROM dual;
检查一下 - >的 Oracle SQL Fiddle
该表是在Oracle DB中创建的。现在根据用户的UID传递,我需要按照下面的状态检索他们的核心:
Click here to view Expected Results
到目前为止,我试图检索结果但无法加入它们。
SELECT STLK.STAT_CODE, STLK.STATUS FROM STATUS_LKUP STLK WHERE STLK.STAT_ID IN (SELECT USRST.STAT_ID FROM USER_STATUS USRST WHERE USRST.UID = 'UID_001');
请帮忙。
仅供参考:这不是家庭作业。实际表格很复杂,只是为了更好的解释而转换它们。
提前谢谢。
答案 0 :(得分:1)
尝试
SELECT l.STATUS,
l.STAT_CODE,
c.CORE_ID
FROM STATUS_LKUP l JOIN CORE c
ON l.STAT_CODE = c.STAT_CODE JOIN USER_STATUS u
ON l.STAT_ID = u.STAT_ID
WHERE u."UID" = 'UID_002'
ORDER BY l.STATUS DESC, l.STAT_CODE
<强> SQLFiddle 强>
答案 1 :(得分:1)
相当直接的加入;
SELECT sl."STATUS", sl."STAT_CODE", c."CORE_ID"
FROM USER_STATUS us
JOIN STATUS_LKUP sl
ON us."STAT_ID" = sl."STAT_ID"
JOIN CORE c
ON c."STAT_CODE" = sl."STAT_CODE"
WHERE "UID" = 'UID_001'
ORDER BY "STATUS", "LKUP_ID"
答案 2 :(得分:0)
此版本的查询有效:
SELECT STLK.STAT_CODE, STLK.STATUS
FROM STATUS_LKUP STLK
WHERE STLK.STAT_ID IN (SELECT USRST.STAT_ID
FROM USER_STATUS USRST
WHERE USRST."UID" = 'UID_001'
);
UID
是Oracle中的保留字(请参阅here),这就是它需要双引号的原因。