The data is drawn from the question table, followed by JOIN's

It is necessary that when question.category> 0 was:

LEFT JOIN detailtousers b ON b.idDetailToUsers = a.QuestionIdUser 

Otherwise, another JOIN

Need something like this:

 IF a.QuestionToUser > 0 THEN LEFT JOIN detailtousers c ON c.idDetailToUsers = a.QuestionToUser ELSEIF a.QuestionToUser = 0 THEN LEFT JOIN detailtousers c ON c.idDetailToUsers = d.AnswerToQuestionIdUser ENDIF 
  • 3
    even if it could be written this way, what data do you expect to receive in the sample? What kind of leaky cube instead of a table? - Yura Ivanov
  • I just gave an example - Jony
  • 3
    the fact of the matter is that you have not given any example. You have described some of your thoughts very roughly, but you did not complete it. I will ask again to explain what data you expect to see in the sample? You can get disparate data with one query in several ways, but there is no desire to guess which option will suit you. Considering your previous questions, in which the majority of respondents guess at the coffee grounds, you either don’t understand what you need and accept answers according to the number of “smart words”, or (more likely) you have a wrong approach to data structures and their processing, which change - Yura Ivanov

1 answer 1

Usually they do the following: JOIN is all three tables, considering that you have a LEFT JOIN you don’t lose anything much - the number of records in the intermediate table is determined by the question . But already in the SELECT constructions, entries are output depending on the value of the field question.category

 SELECT a.QuestionIdUser, IF(a.category > 0, b.number, c.number) AS number FROM question a LEFT JOIN detailtousers b ON b.idDetailToUsers = a.QuestionIdUser LEFT JOIN detailtousers c ON c.idDetailToUsers = a.QuestionIdUser