A single SQL query that will use one SELECT result in the next subquery in the same request
P粉195200437
P粉195200437 2024-04-02 20:05:02
0
1
288

I'm looking for a possibility to (re)use the results of a SQL select to get the values ​​of different sub-selects within the same query

First, I should get $ValueToUse by selecting like this:

SELECT `cb_campaignid` as ValueToUse FROM `#__team_groups` WHERE `id` = '$Value'

For the "ValueToUse" result, I would like to make the next selection in the same SQL query statement if possible.

SELECT coalesce (nullif (SUM(Amount), ''), '0') as general_amount, 
(SELECT SUM(c.cb_stand) FROM #__employees c, #__team_users u where c.user_id=u.user_id and u.group=(select id from #__team_groups where cb_campaignid='$ValueToUse')) as teamleden_tussenstand, 
(select title from #__customer_campaigns where id='$ValueToUse') as title, 
(select id from #__team_groups where cb_campaignid='$ValueToUse') as groupID, 
(select goal from #__customer_campaigns where id='$ValueToUse') as goal
 FROM #__customer_payments WHERE published = 1 and campaign_id = '$ValueToUse'

What is the best way to obtain these results?

P粉195200437
P粉195200437

reply all(1)
P粉645569197

How about using the first result as a variable?

SELECT @ValueToUse:=
(SELECT `cb_campaignid` as ValueToUse FROM `#__team_groups` WHERE `id` = '$Value');

SELECT coalesce (nullif (SUM(Amount), ''), '0') as general_amount, 
(SELECT SUM(c.cb_stand) FROM #__employees c, #__team_users u where c.user_id=u.user_id and u.group=(select id from #__team_groups where cb_campaignid=@ValueToUse)) as teamleden_tussenstand, 
(select title from #__customer_campaigns where id=@ValueToUse) as title, 
(select id from #__team_groups where cb_campaignid=@ValueToUse) as groupID, 
(select goal from #__customer_campaigns where id=@ValueToUse) as goal
 FROM #__customer_payments WHERE published = 1 and campaign_id = @ValueToUse;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!