【问题标题】:Sql Union or Join or both togetherSql Union 或 Join 或两者一起
【发布时间】:2017-02-08 14:19:06
【问题描述】:

我有 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 是两个表中日期的并集,余额计算为:

last balance + AmountIn - AmounOut

【问题讨论】:

  • 您需要JOINUNION 不会在这里工作
  • 感谢您的快速回复,但如何加入并保持日期字段作为一个从两个表中获取日期
  • 看起来像一个完全外连接。但我不认为 MySQL 支持它。
  • FULL OUTER 不直接支持,但可以使用选择加入查询联合所有选择反向加入查询来完成,其中倒置_left.joining_column 为空
  • 查询会是什么样子?

标签: mysql sql join union


【解决方案1】:

试试这个:

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

【讨论】:

  • 嗨,这似乎很有帮助,但是如果字段“AmountIn”和“AmountOut”具有相同的名称:表中的“Amount”
  • 没关系。使用您拥有的任何列名并在其后加上别名。 amount as amountInamount as amountOut
  • 正如我所说,没关系。您可以使用列别名。
  • 非常感谢@GurV,您是完美的,并且表中有 3 个具有相同结构,其中余额是表一个金额 + 表二金额 - 表三金额。请问代码是什么样子的?再次感谢
  • 完美。非常感谢@GurV
【解决方案2】:

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'

这将返回您想要的没有列平衡的表。也许有人可以帮助您完成此专栏

【讨论】:

    【解决方案3】:
    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'
    

    您好,我想使用联合解决这个查询,像这样相当冗余的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多