Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

Mysql: Subtracting values based on two queries

I have the following two queries.

SELECT 
    account_name,SUM(amount) AS AMOUNT1 
FROM 
    entries 
LEFT JOIN 
    accounts ON accounts.id = entries.accounts_id
WHERE 
    side = 'D' AND op_balance_dc = 'D'
GROUP BY 
    accounts.id

Here's the result of the query:

Query1

SELECT 
    account_name,SUM(amount) AS AMOUNT2 
FROM 
    entries 
LEFT JOIN 
    accounts ON accounts.id = entries.accounts_id
WHERE 
    side = 'C' AND op_balance_dc = 'D'
GROUP BY 
    accounts.id

Here's the result of the second query

Query2

I am not trying to display the results of the above two queries, what I am trying to achieve is get the account_name, AMOUNT1 and AMOUNT2 FROM the above queries and then subtract Amount2 FROM AMOUNT1- and then add a value from my table's column.

Example:

(AMOUNT1-AMOUNT2)+op_balance  //here op_balance is my column name

and then display the account_name and (AMOUNT1-AMOUNT2)+op_balance

Could you please help me with this query?

Thanks :)

Please let me know if you need anymore information.:)

Edited

Here's the structure of my tables:

Thanks :)

Edit2

Table Name: entries (Case-1)

  id    trans_id   accounts_id    amount    side
   1      1       12               1000      D
   2      1        1               1000      C
   7      4        1              14000      D
   8      4        2              14000      C

Table Name: entries (Case-2)

  id    trans_id   accounts_id  amount        side
  1           1       12          1000       D
  2           1        1          1000       C

Your code will work for the case-1 but not for case-2. You used lentries.accounts_id = entries.accounts_id in your code and that is why since there's no multiple values for the same accounts_id in the table to join and your code is not counting the values in case-2.

But I am trying to get all the data. Thanks :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the EDITED QUERY for all accounts

select
  a.id,
  a.account_name,
  a.op_balance,
  ifnull(e.AMOUNT1,0) as Amount1,
  ifnull(l.AMOUNT2,0) as Amount2,
  ((ifnull(SUM(e.AMOUNT1),0)-ifnull(l.AMOUNT2,0))+a.op_balance) as Balance
from accounts a
  left join (SELECT
               accounts_id,
               SUM(amount)     AS AMOUNT1
             FROM entries
               LEFT JOIN accounts
                 ON accounts.id = entries.accounts_id
             WHERE entries.side = 'D'
                 AND accounts.op_balance_dc = 'D'
             GROUP BY accounts.id) as e
    on e.accounts_id = a.id
  left join (SELECT
               accounts_id,
               SUM(amount)     AS AMOUNT2
             FROM entries
               LEFT JOIN accounts
                 ON accounts.id = entries.accounts_id
             WHERE side = 'C'
                 AND op_balance_dc = 'D'
             GROUP BY accounts.id) as l
    on l.accounts_id = a.id
group by a.id   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...