2012-09-13 82 views
5

如何使用列值連接表?獲取與列值的表連接

我有三個表如下所列:

  • 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

希望的輸出:

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

即根據列值獲取連接sql查詢。


編輯:

我想取datafrom sql查詢不會形成服​​務器端的編碼。

我試圖從here此查詢,即溫弗雷德的想法(回答)

但是,我無法理解。

msg_by_usertype是基於列,其中值manager那麼就應該選擇manager_table,如果是admin的要admin_table

+0

什麼是你想要檢索的列? –

+0

另外表格是如何相互關聯的?例如消息表和管理表?他們的鏈接欄是什麼 –

+0

我編輯過這個問題,請看看。 – Rafee

回答

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的管理工作。ID應在兩個表中是唯一的。)

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

下,如果我正確地理解你的問題,你想得到這樣的結果嗎?

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