SQL Statistics

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fyrsten
Forum Newbie
Posts: 3
Joined: Fri Jan 06, 2012 9:28 am

SQL Statistics

Post 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
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: SQL Statistics

Post 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
Post Reply