MySQL - Using COUNT(*) in the WHERE clause

前端 未结 9 1692
不思量自难忘°
不思量自难忘° 2020-11-28 20:55

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

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


        
9条回答
  •  温柔的废话
    2020-11-28 21:38

    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;
    

提交回复
热议问题