gpt4 book ai didi

MySQL - 从一个查询的结果中扣除另一个查询的结果

转载 作者:行者123 更新时间:2023-11-29 23:09:16 26 4
gpt4 key购买 nike

我正在运行两个查询。

它们在这里:

第一个查询

SELECT SUM(dbase1.transaction.amount) AS total, dbase1.player.name 
FROM dbase1.transaction
JOIN dev2.player ON dbase1.transaction.player_id = dbase1.player.player_id
WHERE (dbase1.transaction.type !=1)
AND (dbase1.transaction.type !=3)
GROUP BY dbase1.transaction.player_id
ORDER BY name ;

第二个查询

SELECT dbase2.transaction.amount
, dbase2.transaction.player_id
, dbase2.player.name
FROM dbase2.transaction
JOIN dbase2.player ON dbase2.transaction.player_id = dbase2.player.player_id
WHERE dbase2.transaction.provider ='providerx';

在第一个结果集中,我得到一个金额和一个名称,在第二个结果集中,我得到一个金额、一个名称和一个 ID。

我想要的是从名称匹配的第一组结果中扣除第二组结果中的金额。

所以基本上我的最终结果是:

amount from set1 - amount from set2, name, id

我希望这是有道理的,任何建议将不胜感激

谢谢

最佳答案

加入他们吧。

select a.total - b.amount from (
select sum(dbase1.transaction.amount)
as total, dbase1.player.name from dbase1.transaction
JOIN dev2.player on
dbase1.transaction.player_id = dbase1.player.player_id
where
(dbase1.transaction.type !=1)
and
(dbase1.transaction.type !=3) group by dbase1.transaction.player_id
) query_a a
INNER JOIN
(
select dbase2.transaction.amount, dbase2.transaction.player_id, dbase2.player.name from dbase2.transaction
JOIN dbase2.player ON
dbase2.transaction.player_id = dbase2.player.player_id
where dbase2.transaction.provider ='providerx'
) query_b b on a.name = b.name

关于MySQL - 从一个查询的结果中扣除另一个查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28147801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com