首页  >  问答  >  正文

将在同一请求中使用一个SELECT结果在下一个子查询中的单个SQL查询

我正在寻找一种可能性(重新)使用一个 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粉195200437P粉195200437185 天前297

全部回复(1)我来回复

  • P粉645569197

    P粉6455691972024-04-03 17:38:56

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

    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;

    回复
    0
  • 取消回复