根据列值连接具有值的表

时间:2013-08-05 12:51:14

标签: sql ms-access ms-access-2007

我有一张长桌子和一张矮桌:

长表看起来像这样:

LongTable:

+--------------+----------+----------+----------+
| Kabelnummer  |     GL   |more data |even more |           
+--------------+----------+----------+----------+
| 1            |    850   |    x     |    x     |
+--------------+----------+----------+----------+
| 2            |    850   |    x     |    x     |
+--------------+----------+----------+----------+
| 3            |   1300   |    x     |    x     |
+--------------+----------+----------+----------+
| 4            |   1300   |    x     |    x     |
+--------------+----------+----------+----------+

ShortTable:

+--------------+----------+----------+----------+
| data         |     GL   |more data |numericVal|           
+--------------+----------+----------+----------+
|      x       |    850   |    x     | 0.2345   |
+--------------+----------+----------+----------+
|      x       |   1300   |    x     | 0.2849   |
+--------------+----------+----------+----------+

我想要一个查询将“numericVal”列复制到表“LongTable”中,其中GL.Longtable与GL.shorttable相同:

LongTable:

+--------------+----------+----------+----------+----------+
| Kabelnummer  |     GL   |more data |even more |numericVal|          
+--------------+----------+----------+----------+----------+
| 1            |    850   |    x     |    x     | 0.2345   |
+--------------+----------+----------+----------+----------+
| 2            |    850   |    x     |    x     | 0.2345   |
+--------------+----------+----------+----------+----------+
| 3            |   1300   |    x     |    x     | 0.2849   |
+--------------+----------+----------+----------+----------+
| 4            |   1300   |    x     |    x     | 0.2849   |
+--------------+----------+----------+----------+----------+

我该怎么做?

2 个答案:

答案 0 :(得分:1)

尝试使用INNER JOIN加入两个表:

SELECT L.*, s.numericVal 
  FROM LongTable l 
  JOIN sortTable s
    ON l.GL = s.GL

答案 1 :(得分:1)

SELECT lt.Kabelnummer, lt.GL, lt.X, lt.Y, st.numericVal
FROM LongTable lt
INNER JOIN ShortTable st ON lt.GL = st.GL

您使用JOIN来完成此任务。

您可以在此处阅读有关加入的内容:

Join Fundamentals