[英]mysql: merge multiple rows into one
我有兩個表: state_current
和state_snapshots
。 state_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查詢將在第一列中使用state_current
中的A
值,在第二列中以state_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')
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.