gpt4 book ai didi

mysql中的mysql : How to convert multiple row to single row?

转载 作者:可可西里 更新时间:2023-11-01 08:36:25 26 4
gpt4 key购买 nike

我想根据周将多行转换为单行。它应该如下所示。谁能帮帮我?

id      |   Weight   |  Created   |
1 | 120 | 02-04-2012 |
2 | 110 | 09-04-2012 |
1 | 100 | 16-04-2012 |
1 | 130 | 23-04-2012 |
2 | 140 | 30-04-2012 |
3 | 150 | 07-05-2012 |

结果应该是这样的:

id      |   Weight_week1  | Weight_week2  |  weight_week3  | weight_week4  |
1 | 120 | 100 | 130 | |
2 | 110 | 140 | | |
3 | 150 | | | |

提前致谢。

最佳答案

如果这是一个单表那么

SELECT GROUP_CONCAT(weight) as Weight,
WEEK(Created) as Week
Group by Week(Created)

这将为您提供一行,每一行都有周 ID 和逗号分隔的 whights

关于mysql中的mysql : How to convert multiple row to single row?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10480850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com