How can I join tables using a column value?
I have three tables listed below:
messages_table
-----------------------------------------------------------------------------
msg_id | msg_sub | msg_to | to_user_type | msg_from | from_user_type
-----------------------------------------------------------------------------
001 | test | 88 | manager | 42 | admin
002 | test2 | 88 | manager | 94 | manager
admin_table
admin_id | admin_name
001 | Super Admin
manager_table
---------------------------
manager_id | manager_name
---------------------------
88 | Mandela
94 | Kristen
How can I get the desired result, as shown below with an SQL query. That is, the union of tables relative to column values, subject to the following criteria:
If user_type = admin, then he should join admin_table.
If user_type = manager, then he should join manager_table.
Desired conclusion:
-----------------------------------------------------
msg_id | msg_sub | msg_to_name | msg_from_name
-----------------------------------------------------
001 | test | Mandela | Super Admin
002 | test2 | Mandela | Kristen
those. Get the sql connection request based on the column value.
EDIT:
I want the data from the sql query to not form server coding.
I tried this request from here , i.e. Winfred Idea (Answered)
However, I could not understand this.
msg_by_usertype - , manager, manager_table , , admin_table