I have 2 tables.
quiz table
=================
id, q_name, code
completed_quizes table
======================================================
id, completion_date, user_id, quiz_code, quiz_count
I have 2 types of quizes
Table_name: quiz_names
id q_name code
1 grand_quiz gra
2 normal_quiz nor
Table_name: completed_quizes
id completion_date user_id q_code q_count
1 1340841600 (28June2012) 2 gra 0
2 1340755200(27June2012) 2 nor 3
3 1340668800(26June2012) 2 nor 2
4 1340582400(25June2012) 2 nor 1
5 1340496000(24June2012) 2 gra 0
6 1340841600 (28June2012) 3 gra 0
7 1340755200(27June2012) 3 nor 3
8 1340668800(26June2012) 3 nor 2
9 1340582400(25June2012) 3 nor 1
10 1340496000(24June2012) 3 gra 0
Rules:
1- first of all all users grand quiz will be taken.
2- based on grand quiz, if any user last quiz was grand quiz then next suggestion will be normal quiz 1.
3- based on normal quiz 1 , if any user last quiz was normal quiz 1 then next suggestion will be normal quiz 2.
4- based on normal quiz 2 , if any user last quiz was normal quiz 2 then next suggestion will be normal quiz 3.
5- if any users last quiz was normal quiz 3 then again grand quiz will be next suggestion.
P.S
Problem is that every users next suggestion is based upon his previous completed quiz. I want all users who’s what will be the next quiz?
Any advice please?
You need the groupwise maximum to obtain the latest quiz performed by each user, then add 1 (using
MODto rotate back to 0 those who’ve most recently taken quiz 3). To include users who have not yet taken any quiz, you need to make an outer join with your user table:See it on sqlfiddle.