使用列值获取表连接

时间:2012-09-13 09:20:29

标签: sql join

如何使用列值连接表?

我有三个表格如下:

  • 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
    

如何使用SQL查询获得如下所示的所需输出。即 满足以下条件时,连接有关列值的表:

  1. 如果user_type = admin则应加入admin_table

  2. 如果user_type = manager则应加入manager_table

  3. 期望的输出:

    -----------------------------------------------------
    msg_id  | msg_sub   | msg_to_name   | msg_from_name
    -----------------------------------------------------
    001     | test      | Mandela       | Super Admin
    002     | test2     | Mandela       | Kristen
    

    即。根据列值获取join sql查询。


    修改

    我想从sql查询中获取数据,而不是从服务器端编码。

    我从here尝试了这个查询,即Winfred的想法(已回答)

    然而,我无法理解。

    msg_by_usertype是基于列的,其中值manager则应选择manager_table,如果是管理员,则选择admin_table

3 个答案:

答案 0 :(得分:5)

据我了解你的问题,你可以试试这个:

    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,看看它是如何运作的。 (它只适用于你不能拥有像管理员一样具有相同身份的管理员。在两个表中,Id应该是唯一的。)

答案 1 :(得分:3)

请使用以下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 

答案 2 :(得分:2)

如果我理解你的问题,你想得到这样的结果吗?

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

如果是这样,你可以使用这个

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