SQL查询选择映射表值?

时间:2013-05-29 06:57:30

标签: sql sql-server-2008 select-query

我有一个名为Patient的表,其中我有

这样的列
ID Disease1  Disease2  Disease3
         ----------
1     4        3         2
         ----------
2     2        5
         ----------    
3     6
         ----------
4     1

这些是我从表疾病中得到的映射值,其中疾病名称就像

一样
1  hypertension
2  niddm
3  allergy  
4  cough
5  floo
6   vv
etc

现在我想要sql查询来选择

ID Disease1  Disease2  Disease3
           ----------
1   cough    allergy     niddm
           ----------
2   niddm    floo
           ----------
3    vv
           ----------
4     HT

请记住,我有4,5个表映射表,我想要原始值代替所有这些表中的ID。

1 个答案:

答案 0 :(得分:3)

您需要在表Disease上加入表Patient三次,因为Patient中有三列依赖于Disease

SELECT  a.ID,
        b.Disease AS Disease1,
        c.Disease AS Disease2,
        d.Disease AS Disease3
FROM    Patient a
        LEFT JOIN Disease b
            ON a.Disease1 = b.ID
        LEFT JOIN Disease c
            ON a.Disease2 = c.ID
        LEFT JOIN Disease d
            ON a.Disease3 = d.ID

要进一步了解联接,请访问以下链接: