我有三个问题:
SELECT EVENT_TITLE, ORIG_START_DT FROM EVENT_MAIN WHERE USERID='4' AND EVENT_STATUS='P'
SELECT GREET_TITLE, BROADCAST_SCH_DT FROM GREET_MAIN WHERE USERID='4' AND GREET_STATUS='P'
SELECT POLL_TITLE,CREATE_DTTM FROM POLL_MAIN WHERE USERID='4' AND POLL_STATUS='P'
Run Code Online (Sandbox Code Playgroud)
如何将这三个查询组合成单个查询以获得这样的结果.
对于相同的状态,每个USERID可能包含多个行.
EVENT_TITLE | ORIG_START_DT | GREET_TITLE | BROADCAST_SCH_DT | POLL_TITLE | CREATE_DTTM
Run Code Online (Sandbox Code Playgroud)
使用UNION(隐式不同)或UNION ALL类似:
SELECT
EVENT_TITLE "Title",
ORIG_START_DT "DT"
FROM EVENT_MAIN WHERE USERID='4' AND EVENT_STATUS = 'P'
UNION ALL
SELECT
GREET_TITLE,
BROADCAST_SCH_DT
FROM GREET_MAIN WHERE USERID='4' AND GREET_STATUS = 'P'
UNION ALL
SELECT
POLL_TITLE,
CREATE_DTTM
FROM POLL_MAIN WHERE USERID='4' AND POLL_STATUS = 'P';
Run Code Online (Sandbox Code Playgroud)
如果要以下列形式从这三个查询中获取数据:
EVENT_TITLE | ORIG_START_DT | GREET_TITLE | BROADCAST_SCH_DT | POLL_TITLE | CREATE_DTTM
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
SELECT
EVENT_TITLE ,
ORIG_START_DT ,
GREET_TITLE,
BROADCAST_SCH_DT,
POLL_TITLE,
CREATE_DTTM
FROM
(
SELECT *
FROM EVENT_MAIN
WHERE USERID='4' AND EVENT_STATUS = 'P'
) t1
INNER JOIN
(
SELECT *
FROM GREET_MAIN
WHERE USERID='4' AND GREET_STATUS = 'P'
) t2
INNER JOIN
(
SELECT *
FROM POLL_MAIN
WHERE USERID='4' AND POLL_STATUS = 'P'
) t3
Run Code Online (Sandbox Code Playgroud)
这会给你一些类似的东西:
| EVENT_TITLE | ORIG_START_DT | GREET_TITLE | BROADCAST_SCH_DT | POLL_TITLE | CREATE_DTTM |
-------------------------------------------------------------------------------------------
| Title4 | O44 | DTdd | O44 | ddsfTdd | O44 |
| Title4 | O33 | DTdd | O44 | ddsfTdd | O44 |
........
Run Code Online (Sandbox Code Playgroud)
需要注意的是:据我所知,这种查询会以三种方式交叉连接,因为我没有指定JOIN条件,因为你没有在你的问题中确定.如果三个表之间存在任何关系,请JOIN使用该ON子句指定condtion .
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |