连接两个表,其中第二个表用作第一个表的多个列的引用

时间:2014-09-25 05:44:11

标签: c# sql ms-access

我有两个表(这个例子的名称是虚构的):

---------------------------------------
               MainTable
---------------------------------------
Condition_1 | Condition_2 | Condition_3
---------------------------------------
    J       |      H      |     N
    R       |      T      |     
    I       |             |     
    W       |      T      |     
    R       |      M      |     Q

...and so on...


--------------------------------------
              Conditions
--------------------------------------
Condition_Code | Condition_Description
--------------------------------------
      A        | Description goes here
      B        | Description goes here
      C        | Description goes here
      D        | Description goes here
      E        | Description goes here
      F        | Description goes here
      G        | Description goes here

...and so on...

我希望能够从DataSet表中查询Condition_Description Conditions作为{{1}内代码的每个条件的值表格。

因此,当我查看特定行时:

MainTable

如果以前包含代码,我会得到一个空字符串或该条件的描述。

因此,对于上例中的第一行,它将返回ds.Tables["Query"].Rows[i]["Condition_1"].ToString(); 的完整描述而不是字符值。

我尝试了这个查询:

J

...但是,我收到SELECT * FROM MainTable LEFT JOIN Conditions AS C1 ON MainTable.Condition_1 = C1.Condition_Code LEFT JOIN Conditions AS C2 ON MainTable.Condition_2 = C2.Condition_Code LEFT JOIN Conditions AS C3 ON MainTable.Condition_3 = C3.Condition_Code 错误。

missing operator in query expression很难为这个创建标题,因为我的数据库体验有限,所以如果有人有更好的标题,请随时更改。

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,也许这样的事情可以解决问题:

SELECT 
  (
    SELECT FIRST(Condition_Description) FROM Conditions C1 WHERE MT.Condition_1 = C1.Condition_Code  
  ) as C1_Description,
  (
    SELECT FIRST(Condition_Description) FROM Conditions C2 WHERE MT.Condition_2 = C2.Condition_Code  
  ) as C2_Description,
  (
    SELECT FIRST(Condition_Description) FROM Conditions C3 WHERE MT.Condition_3 = C3.Condition_Code  
  ) as C3_Description
FROM MainTable as MT

P.S。不确定访问中第一个函数的语法是什么。条件表设计也不是很优雅:)