mysql - 加入mysql中表之间的差异

标签 mysql sql database join

我有一个名为 cash_billings_bills_articles 的表,它包含原始发票中的项目,另一个 cash_billings_returns_articles 保存新的更改历史发票或退款。

cash_billings_bills_articles 数据: enter image description here

cash_billings_returns_articles 数据: enter image description here

我需要进行连接以获得以下结果: enter image description here

有什么想法吗?谢谢

SQL fiddle :SQL Fiddle Demo

更新:

这是一个小图表演示我需要做什么: enter image description here

最佳答案

这是一个查询,它将获得您问题中出现的结果集。但是,业务规则可能无法完全实现,因为很难仅从输出中解读。此外,这可能最好用过程语言来完成,因为它需要一些跨行的计算。

它利用左右外部连接和联合来获取两个表中的所有行。

select cashbilling_id,
       ifnull(cashbillingreturn_id,min_cashbillingreturn_id) cashbillingreturn_id,
       article_id,
       total,
       diff,
       ifnull(cashbillingreturn_date,min_cashbillingreturn_date) cashbillingreturn_date
from(
      select cashbilling_id,
             ifnull(a_article_id,b_article_id) article_id,
             cashbillingbillarticle_total,
             cashbillingreturnarticle_total,
             ifnull(cashbillingreturnarticle_total,cashbillingbillarticle_total) total,
             ifnull(cashbillingreturnarticle_total,0) - ifnull(cashbillingbillarticle_total,0) diff,
             cashbillingreturn_id,
             cashbillingreturn_date,
             case when @group_id = cashbilling_id then @min_id
                  when @group_id != cashbilling_id then @min_id := cashbillingreturn_id
                  when @group_id := cashbilling_id then @min_id := cashbillingreturn_id
             end min_cashbillingreturn_id,
             case when @date_group_id = cashbilling_id then @min_date
                  when @date_group_id != cashbilling_id then @min_date := cashbillingreturn_date
                  when @date_group_id := cashbilling_id then @min_date := cashbillingreturn_date
             end min_cashbillingreturn_date
      from(
        select a.cashbilling_id,
               a.article_id a_article_id,
               b.article_id b_article_id,
               b.cashbillingreturnarticle_total,
               a.cashbillingbillarticle_total,
               b.cashbillingreturn_id,
               b.cashbillingreturn_date
        from cash_billings_bills_articles a
        left join cash_billings_returns_articles b on (a.cashbilling_id = b.cashbilling_id and a.article_id = b.article_id)
        union
        select b.cashbilling_id, a.article_id a_article_id, b.article_id b_article_id, b.cashbillingreturnarticle_total, a.cashbillingbillarticle_total, b.cashbillingreturn_id, b.cashbillingreturn_date
        from   cash_billings_bills_articles a
        right join cash_billings_returns_articles b on (a.cashbilling_id = b.cashbilling_id and a.article_id = b.article_id)
      ) q join (select @min_id := null as m, @min_date := null as d, @group_id := null as g, @date_group_id := null as dg) v
      order by cashbilling_id, -cashbillingreturn_id desc
    ) q
    where min_cashbillingreturn_id is not null
    order by cashbilling_id, cashbillingreturn_id, abs(diff)
    ;

关于mysql - 加入mysql中表之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332563/

相关文章:

mysql - 保存用于创建数据库的sql代码

javascript - ajax将多条记录插入数据库

PHP 和 MySQL 为同一日期提供两个不同的周数

python 和pymssql

mysql - 我的查询需要什么样的索引?

java - 多图空间问题 : Guava

sql - 如何向通过外键链接的两个表添加数据?

mysql - 根据另一个表中的列对 MySQL 表进行分区

mysql - SQL查询仅在字段等于值的情况下选择最高ID

sql - 如何创建递归查询以获取两个日期之间的所有日期