I have a series of problems all which I can't put in the title but I will try to explain them as best as I can. I am building an online exam platform with PHP and MySQL.

Problem 1: Prevent a record from being randomized. So I have a series of MCQ questions with each question having 4 options. I have these questions and answers randomized for each student so no two students have the same pattern of questions. I am using the ORDER BY RAND() command to archeive this. My problem now comes for questions like this...Example: ***Question: Which of the following is an input device? A) Monitor B) Speaker C) Printer D) None of the above.

My problem is when the system randomizes these answers the option "D" (None of the above) could change to A or B or C which makes the answer invalid since there are answers below it. So is there a workaround which I can use to prevent randomization for option D only while A, B and C are randomized?

Problem 2: Displaying n questions at a time. For MCQ questions where the answers of one question is to be used in the calculation of the next. So let's say I have an MCQ test of questions and question 1 to 5 are related....how can I make the system display these five questions at once instead of 1 question per screen. Again how can I let the user be the one to choosr how many questions are displayed at a time...maybe the user wants n questions displayed at the time such that the value of n is the number of questions to be displayed for each screen and a third option where all the questions are displayed at once.

1 Answer 1


You need to add some flags (or something) to the data.

"None of the above" needs a flag saying it must be last.

Two questions that must be consecutive -- Indicate on the first that a particular question must immediately follow it.

Once you get "business logic" like this, you will probably find that the handling of the flags and the randomization may as well be done in client software, not SQL.

A database is a repository for information. It is not intended as a programming engine. There are simple programming things built into the database (ORDER BY date, ORDER BY rand, filter out stuff; etc), but I suggest that your requirements have crossed the boundary into "gotta do it in a real programming language".

  • Okay. Thanks. I understand what you mean.
    – BlackBeard
    Commented Jun 18, 2019 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.