[英]Use the result from a query table into another query
There are 3 different tables and from first table i get the "contact_id" and based on this i would like to do the SUM into the next 2 tables and then MINUS the tables as in the code bellow. 有3个不同的表,从第一个表开始,我得到“ contact_id”,基于此,我想将SUM放入接下来的2个表中,然后像下面的代码中那样减去这些表。
I am trying to use the result "contact_id" from the first query into the following queries 我正在尝试将第一个查询的结果“ 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
You need to use subquery : 您需要使用子查询:
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
This should solve your problem : 这应该可以解决您的问题:
SELECT (
SELECT SUM(`total_amount`)
FROM `civicrm_contribution`
WHERE `contact_id` in (SELECT `id` FROM `civicrm_contact` WHERE `first_name` LIKE 'test2' ) AS contact_id)
)
-
(
SELECT SUM(`fee_amount`)
FROM `civicrm_participant`
WHERE `contact_id` in (SELECT `id` FROM `civicrm_contact` WHERE `first_name` LIKE 'test2' ) AS contact_id)
) As RemainingPoints
You should limit the result of the subquery to 1 otherwise it will result in an error, the best way is to match the name using '='
instead of 'like'
您应该将子查询的结果限制为1,否则将导致错误,最好的方法是使用
'='
而不是'like'
来匹配名称
SELECT (
SELECT SUM(`total_amount`)
FROM `civicrm_contribution`
WHERE `contact_id`= (SELECT `id` FROM `civicrm_contact` WHERE `first_name` LIKE 'test2' limit 1 ) AS contact_id)
)
-
(
SELECT SUM(`fee_amount`)
FROM `civicrm_participant`
WHERE `contact_id`= (SELECT `id` FROM `civicrm_contact` WHERE `first_name` LIKE 'test2' limit 1 ) AS contact_id)
) As RemainingPoints
This could help you 这可以帮助您
SELECT @s:=1+1 ,@s + 4,@s-1
o/p -@s:=1+1|@s + 4 | o / p-@ s:= 1 + 1 | @s + 4 | @s-1
@ s-1
2 | 6| 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.