tags:

views:

8264

answers:

4

Hey Everyone,

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.

All help is appreciated.

Thank you!

+1  A: 
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
Thanks Joe but that returns the COUNT() - I am looking to return all the gid's which have a COUNT(*) more then 10
There is no need to Min() in there.
yapiskan
+11  A: 

try this;

select gid
from `gd`
group by gid 
having count(*) > 10
order by lastupdated desc
yapiskan
+1 for havingThis 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
Thank you! It works
Fixed my problem as well, thanks so much.
Don Wilson
+1  A: 

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
+2  A: 

try

SELECT DISTINCT gid
FROM `gd`
group by gid
having count(*) > 10
ORDER BY max(lastupdated) DESC
sme
Thank you! It works