3

我正在尝试获取提交的总数,我想要 2 个结果,1 个具有特定状态的提交和 1 个具有所有状态的提交:

SELECT submissions.campid, 
       COUNT(submissions.id) AS subsCountTotal, 
       (SELECT COUNT(submissions.id)
        FROM submissions,campaings  
        WHERE submissions.campid = campaings.id 
        AND campaings.status = 1 
        and submissions.time  >= campaings.startdate 
        and submissions.status = 10
        GROUP BY submissions.campid) AS subsCountngood 
FROM submissions, campaings 
WHERE submissions.campid = campaings.id 
AND  campaings.status = 1
and CASE 
    WHEN campaings.enddate = '0000-00-00' THEN  submissions.time  >= campaings.startdate 
    ELSE  submissions.time <= campaings.enddate
END
GROUP BY submissions.campid ` 

任何帮助将不胜感激:) 谢谢。

4

3 回答 3

1

看看这个子查询

SELECT COUNT(submissions.id)  
FROM submissions,campaings 
WHERE submissions.campid = campaings.id AND  campaings.status = 1 and submissions.time  >= campaings.startdate and submissions.status = 10  
GROUP BY submissions.campid 

当您分组时,您可能会得到多个组,即多个计数。如何在一行中放置多个值。

于 2013-09-16T09:06:21.523 回答
1

您的 CASE 应该导致错误(或者这是 MySQL 特定的扩展)?

恕我直言,您只需要:

SELECT submissions.campid, 
       COUNT(*) AS subsCountTotal, 
       SUM(CASE WHEN submissions.status = 10 THEN 1 ELSE 0 END) AS subsCountngood 
FROM submissions, campaings 
WHERE submissions.campid = campaings.id 
AND  campaings.status = 1
AND 
 (   submissions.time <= campaings.enddate 
  OR 
     (campaings.enddate = '0000-00-00' AND submissions.time  >= campaings.startdate)
 )
GROUP BY submissions.campid
于 2013-09-16T09:14:33.073 回答
0
SELECT submissions.campid, 
       COUNT(submissions.id) AS subsCountTotal, 
       (SELECT COUNT(submissions.id)
        FROM submissions,campaings  
        WHERE submissions.campid = outer_camp.id 
        AND campaings.status = 1 
        and submissions.time  >= outer_camp.startdate 
        and submissions.status = 10
        GROUP BY submissions.campid) AS subsCountngood 
FROM submissions outer_sub, campaings outer_camp
WHERE submissions.campid = campaings.id 
AND  campaings.status = 1
and CASE 
    WHEN campaings.enddate = '0000-00-00' THEN  submissions.time  >= campaings.startdate 
    ELSE  submissions.time <= campaings.enddate
END
GROUP BY submissions.campid 

您需要关联子查询。

于 2013-09-16T09:11:24.130 回答