Trying to get a value from 5 different tables based on another value

后端 未结 1 1641
灰色年华
灰色年华 2021-01-25 20:17

I\'m trying to get the date value from 5 different tables based on the usertype value and display it on DATATABLES table.

F

相关标签:
1条回答
  • 2021-01-25 20:34

    Your question is clearly caused by a column not having the name you expect. However, your query may still not return what you want, because id values could (at least in theory) be shared among tables.

    It is better to move the conditional logic to the on clause:

    SELECT COALESCE(type_1.date, type_2.date, type_3.date, type_4.date, type_5.date) 
           tm.id, tm.usertype
    FROM table_main tm LEFT JOIN
         table_one type_1
         ON tm.id = type_1.uid AND tm.usertype = 1 LEFT JOIN
         table_two type_2
         ON tm.id = type_2.uid AND tm.usertype = 2 LEFT JOIN
         table_three type_3
         ON tm.id = type_3.uid AND tm.usertype = 3 LEFT JOIN
         table_four type_4
         ON tm.id = type_4.uid AND tm.usertype = 4 LEFT JOIN
         table_five type_5
         ON tm.id = type_5.uid AND tm.usertype = 5 ;
    

    This won't fix the specific error you have. For that, you need to fix the column names.

    0 讨论(0)
提交回复
热议问题