How can i join tables using column value?
I have three tables as 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 output as shown below with SQL query. I.e. Join tables with respect to column values when the following criteria is met:
If
user_type = admin
then it should join withadmin_table
.If
user_type = manager
then it should join withmanager_table
.
Desired output:
-----------------------------------------------------
msg_id | msg_sub | msg_to_name | msg_from_name
-----------------------------------------------------
001 | test | Mandela | Super Admin
002 | test2 | Mandela | Kristen
I.e. Get the join sql query based on column value.
EDIT:
I want to fetch the datafrom sql query not form the serverside coding.
I tried this query from here, i.e. Winfred's Idea ( Answered )
However, I could not understand it.
msg_by_usertype
is the column based, where the value manager
then it should select manager_table
and if it is admin the to admin_table