mysql:将多行合并为一行

时间:2017-01-18 14:54:20

标签: mysql

我有两个表:state_currentstate_snapshotsstate_current正好包含4行,即4个不同键的当前值:

+-----+-------+
| key | value |
+-----+-------+
| A   |     1 |
| B   |     2 |
| C   |     3 |
| D   |     4 |
+-----+-------+

现在,我想在state_snapshots中添加一行,其中包含单独列中每个键的值:

+---+---+---+---+
| A | B | C | D |
+---+---+---+---+
| 1 | 2 | 3 | 4 |
| 1 | 2 | 3 | 5 |
| 1 | 2 | 4 | 5 |
 ...
+---+---+---+---+

当然,密钥永远不会在state_current中更改,只会更改值。 mySQL查询将在第一列中的A中创建值为state_current的行,在第二列中的B中创建state_current的值,依此类推?< / p>

我是mySQL的新手,所以提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我能想到的最简单的答案是:

insert into state_snapshots(a,b,c,d)
   values ( (select value from state_current where key='A'),
            (select value from state_current where key='B'),
            (select value from state_current where key='C'),
            (select value from state_current where key='D')
          );
相关问题