gpt4 book ai didi

mysql - 将查询表的结果用于另一个查询

转载 作者:行者123 更新时间:2023-11-29 13:37:12 25 4
gpt4 key购买 nike

有 3 个不同的表,从第一个表中我得到“contact_id”,并基于此我想对接下来的 2 个表进行求和,然后减去表,如下面的代码所示。
我正在尝试将第一个查询的结果“contact_id”用于以下查询

SELECT (
SELECT `id`
FROM `civicrm_contact`
WHERE `first_name` LIKE 'test2'
) AS contact_id

================================================== =============

SELECT (
SELECT SUM(`total_amount`)
FROM `civicrm_contribution`
WHERE `contact_id`=
)
-
(
SELECT SUM(`fee_amount`)
FROM `civicrm_participant`
WHERE `contact_id`=
) As RemainingPoints

最佳答案

您需要使用子查询:

SELECT (
SELECT SUM(`total_amount`)
FROM `civicrm_contribution`
WHERE `contact_id`= (SELECT `id` FROM `civicrm_contact` WHERE `first_name` LIKE 'test2' ) AS contact_id)
)
-
(
SELECT SUM(`fee_amount`)
FROM `civicrm_participant`
WHERE `contact_id`= (SELECT `id` FROM `civicrm_contact` WHERE `first_name` LIKE 'test2' ) AS contact_id)
) As RemainingPoints

关于mysql - 将查询表的结果用于另一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18584046/

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