Reputation: 143
I have a table users
and table questions
. I want to select 5 random users from users
table who has at least 20 queries (db.questions to_user = db.users id) in questions
table. All I've tried:
SELECT *
FROM users s
WHERE exists (SELECT 20
FROM questions m
WHERE m.to_user = s.id)
ORDER BY RAND()
LIMIT 5
Upvotes: 1
Views: 55
Reputation: 1271231
I think this is more what you want:
SELECT u.*
FROM users u
WHERE (SELECT COUNT(*) FROM questions q WHERE q.to_user = u.id) >= 20
ORDER BY RAND()
LIMIT 5;
Upvotes: 2