Jonathan Edgardo Jonathan Edgardo - 1 month ago 8
MySQL Question

Obtaining differences between MySQL tables

Today i need get the differences in two tables together its outcome, this is my scenario.

enter image description here

The desired result I need is the following:

enter image description here

I tryed the following SQL, but i cant get the desired result above:

SELECT
Billings_Returns.billingreturn_id,
Billings_Returns.billin_id,
(Billings_Returns.billingreturn_total - Billings_Bills.billingbill_total) AS billingreturn_total
FROM Billings_Returns
LEFT JOIN Billings_Bills USING (billing_id)

Answer

Thank to @Teja, Solved the query:

SET @total=0;
SELECT billingreturn_id,
       billing_id,
      IF(billingreturn_id<>billing_id, ((@total * -1) + billingreturn_total), billingreturn_total) AS billingreturn_total, @total:=billingreturn_total curr_total
  FROM
     (    SELECT
                billings_Returns.billingreturn_id,
                billings_Returns.billing_id,
                ( billings_returns.billingreturn_total -  
                  billings_bills.billingbill_total
                ) AS billingreturn_total
          FROM billings_returns
         INNER JOIN 
               billings_Bills
            USING (billing_id)
    ) billings_returns;