-3

我有这两张表:

+-------------+--------+      +----+-------+
| employee_id | val_id |      | id | value |
+-------------+--------+      +----+-------+
|         123 |      1 |      |  1 |     A |
+-------------+--------+      +----+-------+
|         123 |      2 |      |  2 |     B |
+-------------+--------+      +----+-------+
|         123 |      3 |      |  3 |     C |
+-------------+--------+      +----+-------+

预期结果

+-------------+-------+-------+-------+ 
| employee_id | val_1 | val_2 | val_3 | ...
+-------------+-------+-------+-------+ 
|         123 |     A |     B |     C |

请问你能帮帮我吗

4

2 回答 2

0
  1. 在第一个表中创建值列
  2. 通过更新子句更新“值”列(您可以使用子选择来选择值)
  3. 删除 val_id 列
  4. DROP 第二张桌子

桌子有多大?

于 2013-06-18T14:00:51.360 回答
0

使用 join 和 pivot 。这将给出所需的结果

SELECT * FROM(
SELECT employeeID,'val_'+CAST(a.val_id AS VARCHAR(2)) val_id ,value 
FROM table1 a INNER JOIN table2 b ON a.val_id=b.val_id
) AS ss
PIVOT
(
    MAX(value)
    FOR [val_id] IN ([val_1],[val_2],[val_3])
)AS pivott
于 2013-07-16T08:48:34.847 回答