将在同一请求中使用一个SELECT结果在下一个子查询中的单个SQL查询
P粉195200437
P粉195200437 2024-04-02 20:05:02
0
1
340

我正在寻找一种可能性(重新)使用一个 SQL 选择结果来获取同一查询内不同子选择的值

首先,我应该通过如下选择获取 $ValueToUse:

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

对于“ValueToUse”结果,如果可能的话,我想在同一个 SQL 查询语句中进行下一个选择。

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'

获得这些结果的最佳方法是什么?

P粉195200437
P粉195200437

全部回复(1)
P粉645569197

使用第一个结果作为变量怎么样?

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;
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板