I need a way for page moderators to view the members of their page ordered by the total number of posts the user has on that particular page. The posts are stored in different tables such as polls, downloads, wiki, etc (like 15 tables). Each of those have the structure of id, user_id, page_id, title, content, etc etc. The table that lists all the users of a page is called 'favorites'. The structure of that is simply id, user_id, page_id, date.
I currently have a query that unions all of the "content tables" together for a specific user to display ALL of their posts across the site regard of what page it is on, I could easily make it specific to each page using WHERE page_id = $page_id). I tried using that code to do what I'm trying to do in the moderator area. As I mentioned, in the moderator area I need to sort the list of users by their total posts count DESC.
I have this code but it is not working.
$getfans = mysql_query("SELECT DISTINCT user_id, ((SELECT id FROM wiki WHERE wiki.page_id = $page_id AND wiki.user_id = favorites.user_id)
UNION ALL
(SELECT id FROM downloads WHERE downloads.page_id = $page_id AND downloads.user_id = favorites.user_id)
UNION ALL
(SELECT id FROM polls WHERE polls.page_id = $page_id AND polls.user_id = favorites.user_id)) AS posts
FROM favorites WHERE favorites.page_id = $page_id AND favorites.status = 0 ORDER BY posts DESC", $conn);
I tried other methods that also did not work but this one made the most logical sense to me so it's the only one I'm going to display
Please help. Thanks.
Additionally...
This was perhaps a step in the right direction
$getfans = mysql_query("SELECT DISTINCT user_id, (SELECT COUNT(id) FROM polls WHERE page_id = $page_id AND user_id = favorites.user_id) AS posts FROM favorites WHERE favorites.page_id = $page_id AND favorites.status = 0 ORDER BY posts DESC", $conn);
When I echo'ed $posts it gave me the correct count for the user's polls for that page.
HOWEVER when I tried to do more than one table it only returned one user.
Example:
$getfans = mysql_query("SELECT DISTINCT user_id, SUM((SELECT COUNT(id) FROM polls WHERE page_id = $page_id AND user_id = favorites.user_id)+(SELECT COUNT(id) FROM wiki WHERE page_id = $page_id AND user_id = favorites.user_id)) AS posts FROM favorites WHERE favorites.page_id = $page_id AND favorites.status = 0 ORDER BY posts DESC", $conn);
I FOUND THE SOLUTION
if curious, just read my post below.