Get table join with column value

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

+5
3

, :

    SELECT msg_id,  
     msg_body,  
     usersBy.userName AS msg_by, 
     usersTo.userName AS msg_to,  
     msg_by_usertype
    FROM messages
    INNER JOIN 
    (SELECT admin_id As id, admin_name as userName
      FROM admin_table         
     UNION         
     SELECT manager_id As id, manager_name as userName
      FROM manager_table ) usersTo ON msg_to = usersTo.id  

     INNER JOIN 
    (SELECT admin_id As id, admin_name as userName
      FROM admin_table        
     UNION         
     SELECT manager_id As id, manager_name as userName
      FROM manager_table ) usersBy ON msg_by = usersBy.id  

SQL Fiddle, , . ( , , . .)

+5

, SQL

SELECT msg_id,  
     msg_body,  
     usersBy.userName AS msg_by, 
     usersTo.userName AS msg_to,  
     msg_by_usertype
FROM messages
INNER JOIN 
    (SELECT admin_id As id, admin_name as userName,'admin' as usertype
      FROM admin_table         
     UNION         
     SELECT manager_id As id, manager_name as userName,'manager' as usertype
      FROM manager_table ) usersTo 
      ON msg_to = usersTo.id  and  msg_by_usertype = usersTo.usertype 
+3

If I understand your question correctly, do you want to get such a result?

MSG_ID      MSG_BODY    MSG_TO      BY           MSG_BY_USERTYPE 
----------  ----------  ----------  -----------  --------------- 
001         test        adm1        managone     manager         
002         sadff       adm1        adm3?        admin   

If so, you can use this

SELECT MSG_ID, MSG_BODY, MSG_TO,                                        
     CASE                                                               
         WHEN MSG_BY_USERTYPE = 'admin' THEN COALESCE(                  
              (SELECT ADMIN_NAME FROM ADMIN_TABLE                       
               WHERE  MSG_BY = ADMIN_ID), RTRIM(MSG_BY) CONCAT '?')     
         WHEN MSG_BY_USERTYPE = 'manager' THEN COALESCE(                
              (SELECT MANAGER_NAME FROM MANAGER_TABLE                   
               WHERE  MSG_BY = MANAGER_ID), RTRIM(MSG_BY) CONCAT '?')   
         ELSE ' '                                                       
     END AS BY,                                                         
 MSG_BY_USERTYPE                                                        
 FROM MESSAGES 
+2
source

All Articles