#1242 - 子查询返回超过1行

时间:2013-09-16 09:00:12

标签: php mysql sql

我正在尝试获取提交的总数,我想要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 ` 

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

3 个答案:

答案 0 :(得分: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 

分组时,您可能会获得多个群组,即多个群组。如何在一行中放置多个值。

答案 1 :(得分: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

答案 2 :(得分: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 

您需要关联子查询。