185

I am trying to accomplish the following in MySQL (see pseudo code)

SELECT DISTINCT gid
FROM `gd`
WHERE COUNT(*) > 10
ORDER BY lastupdated DESC

Is there a way to do this without using a (SELECT...) in the WHERE clause because that would seem like a waste of resources.

shgnInc
  • 1,830
  • 1
  • 22
  • 32

9 Answers9

313

try this;

select gid
from `gd`
group by gid 
having count(*) > 10
order by lastupdated desc
Ali Ersöz
  • 15,422
  • 11
  • 48
  • 63
  • 45
    +1 for having This is *always* the clause that They Don't Bother To Teach Properly on sql courses or books and knowing about it generally the sign that the coder has progressed beyond novice level. – Cruachan Nov 19 '08 at 12:59
  • What if you are trying to use the COUNT() as part of a boolean OR expression? e.g. `AND ((stock = 1 OR quantity > 0) OR (COUNT(v.id) > 0)` – nnyby Oct 14 '11 at 00:27
  • 1
    I figured it out.. you can add to the HAVING clause like so: `HAVING variations > 0 OR (stock = 1 OR quantity > 0)` – nnyby Oct 14 '11 at 00:55
  • 1
    Excellent. I could give it +2 if it was possible. – intumwa Jun 16 '21 at 03:51
30

I'm not sure about what you're trying to do... maybe something like

SELECT gid, COUNT(*) AS num FROM gd GROUP BY gid HAVING num > 10 ORDER BY lastupdated DESC
Greg
  • 307,243
  • 53
  • 363
  • 328
20
SELECT COUNT(*)
FROM `gd`
GROUP BY gid
HAVING COUNT(gid) > 10
ORDER BY lastupdated DESC;

EDIT (if you just want the gids):

SELECT MIN(gid)
FROM `gd`
GROUP BY gid
HAVING COUNT(gid) > 10
ORDER BY lastupdated DESC
Winston Smith
  • 20,979
  • 10
  • 59
  • 75
18

Just academic version without having clause:

select *
from (
   select gid, count(*) as tmpcount from gd group by gid
) as tmp
where tmpcount > 10;
15

try

SELECT DISTINCT gid
FROM `gd`
group by gid
having count(*) > 10
ORDER BY max(lastupdated) DESC
sme
  • 5,623
  • 7
  • 31
  • 30
15

There can't be aggregate functions (Ex. COUNT, MAX, etc.) in A WHERE clause. Hence we use the HAVING clause instead. Therefore the whole query would be similar to this:

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;
Michele La Ferla
  • 6,535
  • 11
  • 48
  • 77
pushkarr
  • 151
  • 1
  • 3
7

COUNT(*) can only be used with HAVING and must be used after GROUP BY statement Please find the following example:

SELECT COUNT(*), M_Director.PID FROM Movie
INNER JOIN M_Director ON Movie.MID = M_Director.MID 
GROUP BY M_Director.PID
HAVING COUNT(*) > 10
ORDER BY COUNT(*) ASC
Mridul Pandey
  • 322
  • 4
  • 5
6

-- searching for weather stations with missing half-hourly records

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;

-- variation of yapiskan with a where .. in .. select

zzapper
  • 4,455
  • 5
  • 47
  • 44
1

i think you can not add count() with where. now see why ....

where is not same as having , having means you are working or dealing with group and same work of count , it is also dealing with the whole group ,

now how count it is working as whole group

create a table and enter some id's and then use:

select count(*) from table_name

you will find the total values means it is indicating some group ! so where does added with count() ;

Gijs
  • 5,108
  • 1
  • 25
  • 42