Filling a database with results from another?
While working with a fine young man from SO last night, I got a great deal of work done on an attendance system for a friend's guild, and now it's all done except for one part.
We made a lot of progress, but the friend that I'm helping had one last request. On each user's page, we would output how many raids the user had been to and missed, but we also output the percent of raids that would be.
Example:
Date Member Attended
8/10/2011 1:开发者_如何学运维44:27pm Testing1234 Yes
8/10/2011 1:44:45pm Testing1234 Yes
8/10/2011 1:44:53pm Testing1234 No
Overall Attendance: 66.666666667%
The way that we compute the percent on each page is done like this (at the time of the user lookup):
$total_query= mysql_query("SELECT rAttend, rTotal FROM users WHERE userName='$v_member'");
$result = mysql_fetch_assoc($total_query);
$percent = $result['rTotal'] == 0 ? 0 : (($result['rAttend'] / $result['rTotal']) * 100);
The friend now wants me to make a new table where I have each member ($v_member) sorted on a page by their raid attendance percentage. I am a bit lost on how to do this, in a way that wouldn't be manual. Can someone help me out with at least a place to start on this?
Thanks a lot!
SELECT userName, rAttend, rTotal, 100 * (rAttend / rTotal) as rAverage
FROM users
ORDER BY rAverage
Would it be acceptable to your friend to show a page that was sorted correctly without there being a separate table for it? If so, you could calculate the percentage in the query:
SELECT (rAttend / rTotal) * 100 as percent_attend FROM users ORDER BY (rAttend / rTotal) DESC
精彩评论