I’m working on a “predict the results” type system where users are asked to predict who will win in 5 categories. I have all the data in a table and I’m trying to figure out a way to count who had the most correct answers. I have an idea of what is needed from the query but my syntax is a mess. Can someone please help me out?
<?php
$res = array(1,2,3,4,5); //correct results
// The idea is to count the number of table fields that match the $res array
// and store that count in the 's' column
$sql = mysql_query("SELECT *,s FROM awards_votes WHERE
s+=IF( c1==".$res[0].",1,0),
s+=IF( c2==".$res[1].",1,0),
s+=IF( c3==".$res[2].",1,0),
s+=IF( c4==".$res[3].",1,0),
s+=IF( c5==".$res[4].",1,0)
ORDER BY s DESC
") or die(mysql_error());
?>
You can simply do:
This works because the boolean expression
(c1 = somevalue)returns a 0 or 1 in MySQL. Adding those together gives the total number of matches.