Mysql - Recursive query between two tables

Asked

Viewed 100 times

0

Situation: Given the profile table (Figure 01), which describes the number, type and year of the questions (Figure 02) for the questionnaire (Figure 03), bring q amount of questions, and the quantity specified in the profile table is random, without repeating and in the sequence described by the table presenting the questionnaire

How to mount this using recursiveness? by scrolling through the profile and filtering the questions?inserir a descrição da imagem aqui

I tried the following code unsuccessfully:

SELECT 
    COALESCE(CatParent.id, Questoes.id) id, 
    COALESCE(CatParent.enunciado, Questoes.enunciado) enunciado,
    perfil.tipo_id
  FROM perfil
  JOIN Questoes ON perfil.tipo_id = Questoes.tipo_id and
       Questoes.ano_id = perfil.ano_id
  LEFT JOIN Questoes AS CatParent ON Questoes.tipo_id = CatParent.id
ORDER BY RAND( )

group by id, enunciado, tipo_id
  • First, the functions myslq_ are obsolete, use PDO or mysqli. Second, show me what you’ve done with your code. Third, I believe your question is more related to SQL than the PHP part, make your problem clear, focus on the difficulty you are having

  • William, I have never used recursion, I will try to explain more calmly the problem. I need to go through each row of the profile table, I need to search for the quantities of questions according to type is year to assemble the questionnaire, these questions are obviously not repeated in this questionnaire. As if it were a loop, while there are tuples in profile, search the amount of questions according to the type is the year, I thought a query giving a select in questions bringing a limit of each tuples in profile. The questions would be drawn randomly obeying type is the year. It became clearer the idea?

  • Post what you’ve tried, your code

  • Follow the code: [code] SELECT COALESCE(Catparent.id, Questoes.id) id, COALESCE(Catparent.enunciation, Questoes.enunciation) enunciated, profile.tipo_id FROM profile JOIN Questoes ON profile.tipo_id = Questoes.tipo_id and Questoes.ano_id = profile.ano_id LEFT JOIN Questoes AS Catparent ON Questoes.tipo_id = Catparent.id ORDER BY RAND( ) group by id, enunciation, tipo_id[/code]

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.