簡體   English   中英

mysql:將多行合並為一個

[英]mysql: merge multiple rows into one

我有兩個表: state_currentstate_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_currentB值創建一行,依此類推?

我是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM