0

I'm doing a little quiz game and i want to randomly select 10 questions divided in 4 different topics, each one divided on theory and practice questions

1) Is there any different way to select random instead of ORDER BY RAND( ) - this way is very slow with

2) How can i change reduce the query to use only one select?

This is what i want to select

  • 1 Theory and 1 Practise questions from subjet 'Conceito de inventário'
  • 1 Theory and 2 Practise questions from subjet 'Mensuração de inventários no reconhecimento'
  • 1 Theory and 2 Practise questions from subjet 'Mensuração de inventários após o reconhecimento'
  • 2 Theory questions from subjet 'Desreconhecimento de inventários' OR nome_assunto

This is my query right now and it is working well:

  (
    SELECT questao_id, titulo_pergunta, abordagem, opcao1, opcao2, opcao3, opcao4, nome_tema, nome_assunto FROM quizz
    INNER JOIN tema ON tema_id = id_tema
    INNER JOIN assunto_quizz ON assunto_id = id_assunto
    WHERE nome_tema = 'IAS 2 Inventários' 
    AND nome_assunto = 'Conceito de inventário'
    AND abordagem = 'T'
    ORDER BY RAND( ) LIMIT 1
    )
    UNION
    (
    SELECT questao_id, titulo_pergunta, abordagem, opcao1, opcao2, opcao3, opcao4, nome_tema, nome_assunto FROM quizz
    INNER JOIN tema ON tema_id = id_tema
    INNER JOIN assunto_quizz ON assunto_id = id_assunto
    WHERE nome_tema = 'IAS 2 Inventários' 
    AND nome_assunto = 'Conceito de inventário'
    AND abordagem = 'P'
    ORDER BY RAND( ) LIMIT 1
    )
    UNION
    (
        SELECT questao_id, titulo_pergunta, abordagem, opcao1, opcao2, opcao3, opcao4, nome_tema, nome_assunto FROM quizz
        INNER JOIN tema ON tema_id = id_tema
        INNER JOIN assunto_quizz ON assunto_id = id_assunto
        WHERE nome_tema = 'IAS 2 Inventários' 
        AND nome_assunto = 'Mensuração de inventários no reconhecimento'
        AND abordagem = 'T'
       ORDER BY RAND( ) LIMIT 1
    ) 
    UNION
    (
        SELECT questao_id, titulo_pergunta, abordagem, opcao1, opcao2, opcao3, opcao4, nome_tema, nome_assunto FROM quizz
        INNER JOIN tema ON tema_id = id_tema
        INNER JOIN assunto_quizz ON assunto_id = id_assunto
        WHERE nome_tema = 'IAS 2 Inventários' 
        AND nome_assunto = 'Mensuração de inventários no reconhecimento'
        AND abordagem = 'P'
       ORDER BY RAND( ) LIMIT 2
    ) 
    UNION
    (
        SELECT questao_id, titulo_pergunta, abordagem, opcao1, opcao2, opcao3, opcao4, nome_tema, nome_assunto FROM quizz
        INNER JOIN tema ON tema_id = id_tema
        INNER JOIN assunto_quizz ON assunto_id = id_assunto
        WHERE nome_tema = 'IAS 2 Inventários' 
        AND nome_assunto = 'Mensuração de inventários após o reconhecimento'
        AND abordagem = 'T'
       ORDER BY RAND( ) LIMIT 1
    ) 
    UNION
    (
        SELECT questao_id, titulo_pergunta, abordagem, opcao1, opcao2, opcao3, opcao4, nome_tema, nome_assunto FROM quizz
        INNER JOIN tema ON tema_id = id_tema
        INNER JOIN assunto_quizz ON assunto_id = id_assunto
        WHERE nome_tema = 'IAS 2 Inventários' 
        AND nome_assunto = 'Mensuração de inventários após o reconhecimento'
        AND abordagem = 'P'
       ORDER BY RAND( ) LIMIT 2
    ) 
    UNION
    (
        SELECT questao_id, titulo_pergunta, abordagem, opcao1, opcao2, opcao3, opcao4, nome_tema, nome_assunto FROM quizz
        INNER JOIN tema ON tema_id = id_tema
        INNER JOIN assunto_quizz ON assunto_id = id_assunto
        WHERE nome_tema = 'IAS 2 Inventários' 
        AND nome_assunto = 'Desreconhecimento de inventários' OR nome_assunto = 'Divulgações sobre inventários'
        AND abordagem = 'T'
       ORDER BY RAND( ) LIMIT 2
    ) 
Johny tota
  • 55
  • 1
  • 9

0 Answers0