I have a table in MySQL database from which I want to select a unique number of rows.My current table has 3 columns named user_id, image_id and is_like. I need to list all the
users for whom the count of user-id is equal to 221. For example, for a particular user 19, the count of 19 must be 221 in the column user_id. I have written the following code, but it gives me a wrong answer in the form:
SELECT * FROM `votes` WHERE COUNT(userid)=221 GROUP BY userid
It gives me an error
1111 – Invalid use of group function.
Your query should be
You need to use
HAVING, notWHERE.The
WHEREclause filters which rows are selected. The rows are then grouped, then the numbers (inuseridcolumn) are the aggregated for theCOUNTfunction.HAVINGis likeWHERE, only it happens after theCOUNTvalue has been calculated.