Sql Union或Join或两者在一起

时间:2017-02-08 14:19:07

标签: mysql sql join union

我有2个表AmountInAmountOut

第一个表格Amountin如下:

AmountIn
+--------+--------------+-----------+
| account| date         | AmountIn  |
+--------+--------------+-----------+
| A      |  2017/2/6    | 200       |
| A      |  2017/2/5    | 100       |
| A      |  2017/2/5    | 500       |
| B      |  2017/2/1    | 1000      |
| B      |  2017/2/1    | 2000      |
| C      |  2017/1/20   | 25        |
+--------+----+---------+-----------+

第二个看起来像:

  AmountOut
+--------+--------------+-----------+
| account| date         |AmountOut  |
+--------+--------------+-----------+
| A      |  2017/2/8    | 200       |
| A      |  2017/2/7    | 100       |
| A      |  2017/2/6    | 500       |
| B      |  2017/2/2    | 1000      |
| B      |  2017/2/1    | 2000      |
| C      |  2017/1/20   | 25        |
+--------+----+---------+-----------+

现在我想要一个显示结果如下的查询:

ForAccountA
+--------+--------------+----------+-----------+------------+
| account| date         | AmountIn | AmountOut | Balancy    |
+--------+--------------+-------- -+-----------+------------+
| A      |  2017/2/5    | 500      | 0         | 500        |
| A      |  2017/2/5    | 100      | 0         | 600        |
| A      |  2017/2/6    |   0      | 500       | 100        |
| A      |  2017/2/6    | 200      | 0         | 300        |
| A      |  2017/2/7    | 0        | 100       | 200        |
| A      |  2017/2/8    | 0        | 200       | 0          |
+--------+----+---------+----------+-----------+------------+
查询中的

date field是两个表中的日期联合,而balancy的计算公式为:

last balance + AmountIn - AmounOut

3 个答案:

答案 0 :(得分:2)

试试这个:

select
    t.*,
    @sum := if(@account = account, 
                @sum + AmountIn - AmountOut,
                if((@account := account) is not null,
                    AmountIn - AmountOut, 0)
            ) balance
from (
    select
        *
    from (
        select
            1 x,
            account,
            date,
            AmountIn,
            0 AmountOut
        from AmountIn
        union all
        select
            0 x,
            account,
            date,
            0 AmountIn,
            AmountOut
        from AmountOut
    ) t order by account, date, x
) t cross join (select @account := null, @sum := 0) t2

编辑:

三张桌子:

select
    t.*,
    @sum := if(@account = account, 
                @sum + amountOne + amountTwo - amountThree,
                if((@account := account) is not null,
                    amountOne + amountTwo - amountThree, 0)
            ) balance
from (
    select
        *
    from (
        select 
            2 x, account, date, amount amountOne, 
            0 amountTwo, 0 amountThree
        from table1
        union all
        select
            1 x, account, date, 0 amountOne,
            amount amountTwo, 0 amountThree
        from table2
        union all
        select
            0 x, account, date, 0 amountOne,
            0 amountTwo, amount amountThree
        from table3
    ) t order by account, date, x
) t cross join (select @account := null, @sum := 0) t2

答案 1 :(得分:-1)

Select
       case when a.account is not null 
            then a.account else b.account
       end as Account, 
       case when a.account is not null then a.date
            else b.date as date, 
       a.AmountIn, b.AmountIn, (a.AmountIn - b.AmountOut) as balance
       from AmountIn 
a  left join ForAccontA on a.account = b.account where a.account = 'A' and 
b.account = 'A'

您好我想使用union

来解决这个相当冗余的代码

答案 2 :(得分:-1)

MSSQL:

select ai.account,
       ai.date,ai.AmountIn, 
           case when AmountIn is NULL then 0 
           else 0 
       end as AmontOut  
from AmountIn as ai
where ai.account = 'A'
union
select ao.account,
       ao.date,
       case 
           when AmountOut is NULL then 0 
           else 0 
       end as AmountOut, 
      ao.AmountOut 
from AmountOut as ao
where ao.account = 'A'

这将返回您没有列balancy的所需表。也许有人会帮助你解决这个专栏