[英]mysql: merge multiple rows into one
I have two tables: state_current
and state_snapshots
. 我有两个表: state_current
和state_snapshots
。 state_current
contains exactly 4 rows, the current values for 4 different keys: state_current
包含4行,这是4个不同键的当前值:
+-----+-------+
| key | value |
+-----+-------+
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
+-----+-------+
Now, I want to add a row to state_snapshots
that contains the values of each key in a seperate column: 现在,我想向state_snapshots
添加一行,其中在单独的列中包含每个键的值:
+---+---+---+---+
| A | B | C | D |
+---+---+---+---+
| 1 | 2 | 3 | 4 |
| 1 | 2 | 3 | 5 |
| 1 | 2 | 4 | 5 |
...
+---+---+---+---+
Of course, the keys never change in state_current
, only the values. 当然,键永远不会在state_current
更改,而只会更改值。 What mySQL-query will create a row with the value of A
in state_current
in the first column, the value of B
in state_current
in the second and so on? 哪种mySQL查询将在第一列中使用state_current
中的A
值,在第二列中以state_current
的B
值创建一行,依此类推?
I'm new to mySQL, so thanks in advance for any help! 我是mySQL的新手,所以在此先感谢您的帮助!
The simplest answer I can think about is: 我能想到的最简单的答案是:
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')
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.