【发布时间】:2021-11-12 09:17:26
【问题描述】:
我正在尝试编写一个 MySQL 选择语句,它将使用 table1 和 table2 生成一个 table3。
表1
| id | Month | Year | Name | Length | Breath |
|---|---|---|---|---|---|
| 1 | January | 2002 | Square | 5 | 2 |
| 2 | February | 2003 | Circle | 6 | 3 |
| 3 | March | 2004 | Cylinder | 7 | 4 |
| 4 | April | 2005 | Cube | 8 | 5 |
| 5 | May | 2006 | Quadilateral | 9 | 6 |
表2
| id | Month | Year | Name | Frequency | Area | Volume |
|---|---|---|---|---|---|---|
| 1 | January | 2002 | Square | 1 | 20 | 50 |
| 2 | Febrauy | 2003 | Circle | 2 | 25 | 55 |
| 3 | March | 2004 | Cylinder | 3 | Null | 60 |
| 4 | April | 2005 | Cube | 4 | 35 | 65 |
| 5 | May | 2006 | Quadilateral | 5 | 40 | Null |
table3生成的表应该是这样的。
| Name | Type | Month | Year | Length | Breath | Frequency | Volume/Area | Cum_Area | Cum_Volume | Area | Volume |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Square | 2d | January | 2002 | 5 | 2 | 1 | 2.5 | 20 | 50 | 20 | 50 |
| Circle | 2d | February | 2003 | 6 | 3 | 2 | 2.2 | 45 | 105 | 25 | 55 |
| Cylinder | 3d | March | 2004 | 7 | 4 | 3 | 2 | 45 | 165 | 30 | 60 |
| Cube | 3d | April | 2005 | 8 | 5 | 4 | 1.86 | 80 | 230 | 35 | 65 |
| Quadilateral | 4d | May | 2006 | 9 | 6 | 5 | 1.75 | 120 | 230 | 40 | 70 |
在我尝试过的语句中,每当累积列遇到第一个空值时,从那时起的每一行都是空的。
另外,我如何写类型列?我希望方形和圆形是“2d”;立方体和圆柱体为“3d”,四边形为“4d”。
表 1 和表 2 具有共同的名称、月份和年份列。
【问题讨论】:
-
使用它们共有的列连接两个表。对
Type列使用CASE表达式。 -
类型的条件是什么?你的逻辑是什么?请阅读教程,stackoverflow.com/help/how-to-ask 和 meta.stackoverflow.com/questions/333952/…
-
为什么是四边形4d?在几何学中,四边形是具有 4 条边的二维形状。
-
这只是虚拟数据和数字,因为我无法分享真实数据而且它太大了。
标签: mysql