Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Using COUNT(*) in the WHERE clause

People also ask

Can we use COUNT in WHERE clause in MySQL?

SQL SELECT COUNT() can be clubbed with SQL WHERE clause. Using the WHERE clause, we have access to restrict the data to be fed to the COUNT() function and SELECT statement through a condition.

What is COUNT (*) in MySQL?

MySQL COUNT() Function The COUNT() function returns the number of records returned by a select query.

Can we use WHERE in COUNT?

The WHERE clause can be used along with SQL COUNT() function to select specific records from a table against a given condition.

How do I COUNT the number of rows returned by a query in MySQL?

The COUNT() function is an aggregate function that returns the number of rows in a table. The COUNT() function allows you to count all rows or only rows that match a specified condition. The COUNT() function has three forms: COUNT(*) , COUNT(expression) and COUNT(DISTINCT expression) .


try this;

select gid
from `gd`
group by gid 
having count(*) > 10
order by lastupdated desc

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

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

Just academic version without having clause:

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

try

SELECT DISTINCT gid
FROM `gd`
group by gid
having count(*) > 10
ORDER BY max(lastupdated) DESC