根据列值连接表。

时间:2022-12-07 13:35:05

i have 1 long table and 1 short table:

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

the long table looks like this:

长桌子是这样的:

LongTable:

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

and

ShortTable:

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

I would like a query that copies the column "numericVal" into the table "LongTable" where GL.Longtable is the same as GL.shorttable:

我想要一个将“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   |
+--------------+----------+----------+----------+----------+

How do I do this?

我该怎么做呢?

2 个解决方案

#1


1  

Try to join both tables with INNER JOIN:

尝试将两个表连接到内部连接:

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

#2


1  

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

You use a JOIN to accomplish this.

您使用一个连接来完成这个任务。

You can read about joins here:

你可以在这里读到关于连接的内容:

Join Fundamentals

加入基本面

#1


1  

Try to join both tables with INNER JOIN:

尝试将两个表连接到内部连接:

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

#2


1  

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

You use a JOIN to accomplish this.

您使用一个连接来完成这个任务。

You can read about joins here:

你可以在这里读到关于连接的内容:

Join Fundamentals

加入基本面