1

I have a question game quiz in my SQL query as

  • question 1 to 10
  • I use sql random query to select question
  • use array push to keep previous question
  • use sql query [query from array push] to select new question (no more previous question show up)

FIRST QUESTION START

  • 1: SELECT * FROM questions WHERE catid="9" ORDER BY RAND() LIMIT 0 , 1
  • 2: SQL query get id=2 so array push = (2)

SECOND QUESTION START (if answer correct)

  • 1: SELECT * FROM questions WHERE catid="9" AND ( id <> "2" ) ORDER BY RAND() LIMIT 0 , 1
  • 2: SQL query get id=5 so array push = (2,5)

THIRD QUESTION START (if answer correct)

  • 1: SELECT * FROM questions WHERE catid="9" AND ( id <> "2" AND id <> "5" ) ORDER BY RAND() LIMIT 0 , 1
  • 2: SQL query get id=1 so array push = (2,5,1)

    ... QUESTION START (if answer correct)

  • 1: ....

  • 2: ....

So I just want to know if I have 1000 questions so my SQL query going to:

SELECT * FROM questions WHERE catid="9" AND ( id <> "2" AND id <> "5" AND id <> "1" AND .... AND id <> "999" ) ORDER BY RAND() LIMIT 0 , 1

This should be slow for page process or not?.

If slow, should we have another way to process this query? thanks....

3
  • I don't think it is slowing the page that much... I thought you have a problem and not just asking for opinion. My bad. Commented Mar 24, 2016 at 8:43
  • First of all, change this: id <> 2 and id <> 5 and... to this: id NOT IN (2,5,1...) and you can store them as an array and check against it. Commented Mar 24, 2016 at 8:47
  • 1000 is really not that much in database terms. 100 000, still not that much. Maybe a bigger concern would be getting query size limits errors with this approach. However doing something akin to this is probably unavoidable based on your requirements (unless you're willing to create a temporary table for each user that keeps track on unasked questions only). Commented Mar 24, 2016 at 8:55

3 Answers 3

1

What about getting the entire array of questions at the start, and choosing a random question from that (using a random number times the length of the array) and after asking it - either just remove it from the array or move it to a different array - maybe"questionsAsked" or something?that way you do not need to continually keep adding to the list of exclusions - you simply select from the remaining questions? and the questionsAsked array keeps the list of the asked questions.

Sign up to request clarification or add additional context in comments.

Comments

0

Push the id of the previous question to an array.

$array[] = $previous_id;

Select a random question with id not in $array.

$ids = join(',',$array);

$sql = "SELECT * FROM questions WHERE catid="9" AND id NOT IN ($ids) ORDER BY RAND() LIMIT 1";

Repeat this process while the answer is correct.

edit : Also, you should properly sanitize the inputs to prevent SQL injection

1 Comment

NOT IN ($array) should be NOT IN ($ids)
0

I have not tried it but you can use DISTINCT to query different values.

SELECT DISTINCT id, catid, ... FROM questions WHERE catid="9" ORDER BY RAND() LIMIT 10

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.