[Solved] Mysql display random 4 users with more than 5 articles


I’ve mocked up some table data to test my query. WHERE clauses must be positioned after JOINs. You are also a little ambiguous about the comparison of COUNT AND 5 — if you want more than 5 then >5, if you want 5 or more then >=5.

SQL: (SQLFiddle Demo)

SELECT a.user_id,a.username,COUNT(b.user_id)
FROM users a
INNER JOIN articles b ON a.user_id=b.user_id
WHERE a.type=3
GROUP BY a.user_id 
HAVING COUNT(b.user_id)>5
ORDER BY RAND()
LIMIT 4

0

solved Mysql display random 4 users with more than 5 articles