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.
Answers
try this;
select gid
from `gd`
group by gid
having count(*) > 10
order by lastupdated desc
try
SELECT DISTINCT gid
FROM `gd`
group by gid
having count(*) > 10
ORDER BY max(lastupdated) DESC
Just academic version without having clause:
select *
from (
select gid, count(*) as tmpcount from gd group by gid
) as tmp
where tmpcount > 10;
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;
0 comments:
Post a Comment