mysql:将多行合并为一行
我有两个表:mysql:将多行合并为一行,mysql,Mysql,我有两个表:state\u current和state\u snapshots状态_当前正好包含4行,4个不同键的当前值: +-----+-------+ | key | value | +-----+-------+ | A | 1 | | B | 2 | | C | 3 | | D | 4 | +-----+-------+ 现在,我想在state\u snapshots中添加一行,该行在一个单独的列中包含每个键的值: +---+---+--
state\u current
和state\u snapshots
<代码>状态_当前正好包含4行,4个不同键的当前值:
+-----+-------+
| key | value |
+-----+-------+
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
+-----+-------+
现在,我想在state\u snapshots
中添加一行,该行在一个单独的列中包含每个键的值:
+---+---+---+---+
| A | B | C | D |
+---+---+---+---+
| 1 | 2 | 3 | 4 |
| 1 | 2 | 3 | 5 |
| 1 | 2 | 4 | 5 |
...
+---+---+---+---+
当然,键在当前状态下不会改变,只会改变值。哪个mySQL查询将在第一列中创建一个值为state\u current
的行,在第二列中创建值为B
的行,依此类推
我是mySQL新手,所以提前感谢您的帮助 我能想到的最简单的答案是:
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')
);