Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用带有count的子查询_Mysql_Sql_Subquery - Fatal编程技术网

Mysql 使用带有count的子查询

Mysql 使用带有count的子查询,mysql,sql,subquery,Mysql,Sql,Subquery,我试图统计今年(2014年)任何工作中假设超过三次的所有人员。按月分组。需要记住的一件事是表的大小是72GB,因此我正在努力使查询尽可能高效。我正在使用下面的查询,但它没有给我任何结果。有人能告诉我哪里不对,或者最好的方法是什么 select month(postulationdate), count(idpostulant) from postulations where postulationdate >= '2014-01-01' and idpostulant = (select

我试图统计今年(2014年)任何工作中假设超过三次的所有人员。按月分组。需要记住的一件事是表的大小是72GB,因此我正在努力使查询尽可能高效。我正在使用下面的查询,但它没有给我任何结果。有人能告诉我哪里不对,或者最好的方法是什么

select month(postulationdate), count(idpostulant) from postulations
where postulationdate >= '2014-01-01'
and idpostulant = (select count(idpostulant) >= 3)
group by 1

如果有任何问题,我很乐意回答。

您可以使用
HAVING
子句根据聚合进行筛选:

SELECT month(postulationdate), count(idpostulant) 
FROM postulations
WHERE postulationdate >= '2014-01-01' 
GROUP BY month(postulationdate)
HAVING count(idpostulant) >= 3
如果在
positionDate
上有索引,它将加快在
WHERE
子句中进行的初始筛选

更新:您可能表示2014年总计>=3,然后按月细分,您可以这样做:

SELECT month(postulationdate), count(idpostulant) 
FROM postulations AS a
WHERE postulationdate >= '2014-01-01' 
  AND EXISTS(SELECT idpostulant 
             FROM postulations AS b
             WHERE postulationdate >= '2014-01-01' 
                AND a.idpostulent = b.idpostulent
             GROUP BY idpostulant
             HAVING count(idpostulant) >= 3)                    
 GROUP BY month(postulationdate)

太好了,我一边写一边用。此表唯一的缺点是运行简单查询所需的时间。@Pietro请参阅更新,我相信我最初错误地解释了您的问题。第一个查询将返回每月>=3条记录的人。