I'm trying to create a leaderboard of members on my site by using the two nested queries below.
The first query grabs a list of members:
SELECT member_id, username, screen_name FROM exp_members WHERE group_id IN (1,5) LIMIT 100
The second query is nested inside the first query, and grabs a count of how many times a member's entries have been favorited:
SELECT COUNT(*) AS favorite_count
FROM qb_channel_titles, qb_channel_data, qb_matrix_data
WHERE qb_channel_titles.channel_id = '1'
AND qb_channel_titles.entry_id = qb_channel_data.entry_id
AND qb_channel_titles.entry_id = qb_matrix_data.entry_id
AND field_id = '13'
AND author_id = 'MEMBER_ID_FROM_FIRST_QUERY'
ORDER BY favorite_count DESC"
}
So the code I have is like:
[first query]
[second query]
..output one row of the leaderboard..
[/second query]
[/first query]
Nesting the second query inside the first gives me the proper list of members and the number of votes they've each received, but the list is sorted on the first (outer) query rather than the second (inner) query.
Two questions:
- How do I sort the list of members by
favorite_count
in descending order? - What is the most resource efficient way to do this? I suspect that nesting queries isn't the best way to go, but I honestly don't know any better.