簡體   English   中英

SQL從具有不同列名的兩個不同表中選擇行

[英]SQL select rows from two different tables with different column names

我想從兩個不同的表(TABLE_A和TABLE_B)中選擇行。 我正在使用oracle db和hibernate。 這是我的桌子:

這是TABLE_A

+----+--------+---------+-----------+---------------------+---------------------+
| ID | AMOUNT | BALANCE |  STATUS   |     CREATE_DATE     |      END_DATE       |
+----+--------+---------+-----------+---------------------+---------------------+
|  1 |      5 |       5 | FINISHED  | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
|  2 |     12 |      17 | TIMED_OUT | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
|  3 |     21 |      38 | COMMITED  | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
+----+--------+---------+-----------+---------------------+---------------------+

這是TABLE_B

+----+----------------+---------------+----------------+---------------------+
| ID | PAYMENT_AMOUNT | TOTAL_BALANCE | PAYMENT_STATUS |  REQUEST_TIMESTAMP  |
+----+----------------+---------------+----------------+---------------------+
|  1 |              3 |            23 | FAILED         | 2014-02-27 15:10:20 |
|  2 |             12 |            11 | FULFILLED      | 2014-02-27 15:10:20 |
|  3 |              2 |             9 | TIMED_OUT      | 2014-02-27 15:10:20 |
+----+----------------+---------------+----------------+---------------------+

我想做的是,我需要通過單個查詢獲得如下結果:

+--------+---------+-----------+---------------------+---------------------+
| AMOUNT | BALANCE |  STATUS   |        DATE         |      END_DATE       |
+--------+---------+-----------+---------------------+---------------------+
|      5 |       5 | FINISHED  | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
|     12 |      17 | TIMED_OUT | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
|     21 |      38 | COMMITED  | 2014-02-27 15:10:20 | 2014-02-27 15:10:20 |
|      3 |      23 | FAILED    | 2014-02-27 15:10:20 | null                |
|     12 |      11 | FULFILLED | 2014-02-27 15:10:20 | null                |
|      2 |       9 | TIMED_OUT | 2014-02-27 15:10:20 | null                |
+--------+---------+-----------+---------------------+---------------------+

結果ordered by DATE 列描述如下:

AMOUNT = TABLE_A.AMOUNT or TABLE_B.PAYMENT_AMOUNT
BALANCE = TABLE_A.BALANCe or TABLE_B.TOTAL_BALANCE
STATUS = TABLE_A.STATUS or TABLE_B.PAYMENT_STATUS
DATE = TABLE_A.CREATE_DATE or TABLE_B.REQUEST_TIMESTAMP
END_DATE = TABLE_A.END_DATE or null

有什么辦法可以在oracle db中獲得這些結果以及將這些結果綁定到java中的休眠對象?

嘗試這樣的事情

select * from (
select id as amount, ... from t1
union all 
select id as amount, ... from t2
) t order by date

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM