Is there a better way to write the following query?

SELECT COUNT(*) as votes, combined.char_name 
FROM (
        SELECT vote_id, c.char_name 
        FROM database.votes vote 
        INNER JOIN database.charities c 
                ON vote.vote_char_id=c.char_id 
        WHERE vote.vote_voter_id=' . intval($_POST['voter_id']) . '
        
        UNION ALL
        
        SELECT varc_id, c.char_name 
        FROM database.votes_archive varc 
        INNER JOIN database.charities c 
                ON varc.varc_char_id=c.char_id 
        WHERE varc.varc_voter_id=' . intval($_POST['voter_id']) . ' 
        ) combined  
GROUP BY combined.char_name


There are three tables: charities, today's votes, and a vote archive. The votes 
and votes_archive tables have the same columns (with names). The results I am 
looking for is a list of all the charities the individual has voted for with a 
count of how many times he has voted for each. So, if he has voted for the Boys 
and Girls Club twice, the MS Support Foundation seven times, and the BSA once, 
it'd return:

2, Boys and Girls Club
7, MS Support Foundation
1, BSA


The query above works and returns that information. Is that the best way to do 
that though?

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to