获得两个虚拟列的数量之差-MySQL

问题描述 投票:0回答:3

我想得到两栏之间的区别,这是我当前的查询:

select customer_id as customers_id, sum(amount) as manual_amount,
       (select amount from customers where id = customers_id) as current_wallet_amount
FROM wallet_transactions 
where customer_id = 14438 
group by wallet_transactions.customer_id

我得到的结果附在下面:

enter image description here

我需要的是人工金额和current_wallet_amount之差。任何帮助都是非常可贵的

mysql sql sql-server subquery
3个回答
0
投票
SELECT
  wt.customer_id AS customers_id, 
  SUM(wt.amount) AS manual_amount, 
  ANY_VALUE(customers.amount) AS current_wallet_amount,
  (SUM(wt.amount) - ANY_VALUE(customers.amount)) AS diff_amount
FROM wallet_transactions AS wt
JOIN customers ON customers.id = wt.customer_id
WHERE customer_id = 14438 
GROUP BY wt.customer_id

1
投票

您可以在下面尝试-


0
投票
SELECT cu.id AS customers_id, 
       SUM(wt.amount) AS manual_amount, 
       cu.amount AS current_wallet_amount,
       SUM(wt.amount) - cu.amount AS difference
FROM wallet_transactions AS wt
JOIN customers cu ON cu.id = wt.customer_id
WHERE cu.id = 14438 
GROUP BY cu.id, cu.amount
© www.soinside.com 2019 - 2024. All rights reserved.