HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Select random 10 unique rows from MySQL database?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
randomuniquerowsdatabasemysqlselectfrom

Problem

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?

Solution

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.

  • question - Hold all questions



  • student_answers - hold student specific answers of each question.



  • 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.

Context

StackExchange Database Administrators Q#91685, answer score: 3

Revisions (0)

No revisions yet.