MySQL – 在WHERE子句中使用COUNT(*)
我正在尝试在MySQL中完成以下操作(请参阅pseudo
代码)
SELECT DISTINCT gid FROM `gd` WHERE COUNT(*) > 10 ORDER BY lastupdated DESC
有没有办法做到这一点,而不是在WHERE子句中使用(SELECT …),因为这看起来像是浪费资源。
所有的帮助表示赞赏。
谢谢!
尝试这个;
select gid from `gd` group by gid having count(*) > 10 order by lastupdated desc
我不知道你在做什么…也许是这样的
SELECT gid, COUNT(*) AS num FROM gd GROUP BY gid HAVING num > 10 ORDER BY lastupdated DESC
尝试
SELECT DISTINCT gid FROM `gd` group by gid having count(*) > 10 ORDER BY max(lastupdated) DESC
SELECT COUNT(*) FROM `gd` GROUP BY gid HAVING COUNT(gid) > 10 ORDER BY lastupdated DESC;
编辑(如果你只是想要gids):
SELECT MIN(gid) FROM `gd` GROUP BY gid HAVING COUNT(gid) > 10 ORDER BY lastupdated DESC
只是没有条款的学术版本:
select * from ( select gid, count(*) as tmpcount from gd group by gid ) as tmp where tmpcount > 10;
– search缺less半小时logging的气象站
SELECT stationid FROM weather_data WHERE `Timestamp` LIKE '2011-11-15 %' AND stationid IN (SELECT `ID` FROM `weather_stations`) GROUP BY stationid HAVING COUNT(*) != 48;
– yapiskan的变化与一个where .. in .. select
在WHERE子句中不能有聚合函数(例如COUNT,MAX等)。 因此我们使用HAVING子句。 因此,整个查询将类似于:
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value;
我认为你不能where
添加count()
。 现在看到为什么….
having
意思,就是说你在工作或者处理团队和统计工作,它也是处理整个团队的,
现在它是如何算作整个小组的工作
创build一个表并input一些id,然后使用:
select count(*) from table_name
你会发现总价值意味着它是指示一些组! 所以where
添加count()
;