MySQL - Using COUNT(*) in the WHERE clause

前端 未结 9 1650
不思量自难忘°
不思量自难忘° 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:50

    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
    
    0 讨论(0)
  • 2020-11-28 21:50

    -- 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

    0 讨论(0)
  • 2020-11-28 21:53

    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
    
    0 讨论(0)
提交回复
热议问题