-5

我有两张桌子:

订金

accountNumber    balance    addDate
0000102          5000       21/01/2013
0000105          5500       21/01/2013
0010024          9800       21/01/2013
0000102          2500       22/01/2013
0000105          9000       22/01/2013
.......          ....       ../../....   //rest of the records

退出

accountNumber    balance    addDate
0000102          2500       20/01/2013
0010024          5000       20/01/2013
0000105          1500       20/01/2013
.......          ....       ../../....  //rest of the records

现在,我需要将这两个表的结果显示为:

细节

date          withdrawal   deposite
20/01/2013    2500
21/01/2013                 5000
22/01/2013                 2500

对于特定的accountNumber=0000102order by addDate.

我是数据库的新手。

4

2 回答 2

2

基本上,您需要在UNION ALL此处使用以保留结果的重复项。

SELECT addDate AS date, balance
FROM
    (
        SELECT  accountNumber, balance, addDate FROM deposit
        UNION ALL
        SELECT  accountNumber, balance, addDate FROM withdrawal
    ) s
WHERE   accountNumber = '0000102'
ORDER   BY date
于 2013-01-26T07:41:34.673 回答
1

或者,对 JW 对您评论中要求的列别名的回答稍作更改:

SELECT addDate AS date, w as withdrawal, d as deposit
    FROM
        (
            SELECT  accountNumber, '' as w, balance as d, addDate FROM deposit
            UNION ALL
            SELECT  accountNumber, balance as w, '' as d, addDate FROM withdrawal
        ) s
    WHERE   accountNumber = '0000102'
    ORDER   BY date
于 2013-01-27T09:27:55.077 回答