mysql从另一个表创建一个表
我想从另一个表创建一个表。我的表a如下:mysql从另一个表创建一个表,mysql,sql,pivot,Mysql,Sql,Pivot,我想从另一个表创建一个表。我的表a如下: TABLE A id year month value 1 2020 1 5* 2 2020 2 2** 3 2019 5 15*** ... 我希望通过mysql将该表创建到该模型中: TABLE B id year jan feb mar apr may ... 1 2020 5* 2** 7 5 4 2
TABLE A
id year month value
1 2020 1 5*
2 2020 2 2**
3 2019 5 15***
...
我希望通过mysql将该表创建到该模型中:
TABLE B
id year jan feb mar apr may ...
1 2020 5* 2** 7 5 4
2 2019 1 4 3 1 15***
...
我可以用php编写代码,但我想用mysql编写,这样我就可以为laravel创建一个迁移文件,这将由管理员自动管理
我知道我应该发布我尝试过的东西,但正如我所说的,我更像是一个php程序员,我知道如何用php来做。我已经检查了如何从另一个表创建一个表,但没有找到一个解决方案来解释何时需要在新表中填写多条记录。您可以使用条件聚合:
select id, year,
max(case when month = 1 then value end) as jan,
max(case when month = 2 then value end) as feb,
max(case when month = 3 then value end) as mar,
. . .
from t
group by id, year;
这是一个透视表,你必须做一些像这样的东西好的,我来试试,谢谢你的帮助