Page 1 of 1

SQL Statistics

Posted: Thu Jan 19, 2012 9:39 am
by fyrsten
Hi there!

I've been making a quiz, and the core of the quiz is done... What i need now is this:

I got a user registration, and in my quiz when a user has answered correctly on an answer, he should be able to see how many wrong answers there has been to that question.

So far i've been able to check all the correct answers the user has made by joining the 'answers' table with the 'question' table... And checking it that way...

What i would like to be able to know is top-5 of the wrong answers on a question... I got a the table called 'answers' and i know the question_id in it... So how do i the wrongs answers and then find out which of those has been answered most times?

Sorry this is very confusing to explain. Hope you understand, and thanks for reading!

Best regards,
Fyrsten

Re: SQL Statistics

Posted: Fri Jan 20, 2012 3:28 pm
by tr0gd0rr
You probably want to use COUNT(*) and ORDER BY COUNT(*) DESC. For example:

SELECT answers.value, COUNT(*) AS num_times_wrong
FROM answers
WHERE answers.question_id = $id
AND answers.is_correct = '0'
GROUP BY answers.value
ORDER BY COUNT(*) DESC
LIMIT 5