Mysql – Select random 10 unique rows from MySQL database

MySQLrandom

I have one table (questions) with 54 questions.

The students have to answer all 54 questions.

I have to show 10 questions at once, then next 10 and so on….

I have to select 10 questions randomly and next 10 random questions except the previous 10 questions that already answered, and so on….

I store all the answers in another table named user_answer.

what will be the MySQL Query?

Best Answer

You can do this by using following query.

SELECT * FROM questions WHERE question_id NOT IN (SELECT question_id FROM student_answers WHERE student_id = '5') ORDER BY RAND() LIMIT 10;

I have assumed that there is 3 tables.

  1. question - Hold all questions
  2. student_answers - hold student specific answers of each question.
  3. student - hold student informations.

I expect that student_id is the primary key of student table and foreign key of student_answer table.

The student_is is brought from session or somewhere.