简体   繁体   English

无法弄清楚如何编写 MySQL 查询

[英]Cannot figure out how to write MySQL Query

I am trying to write a MySQL select statement which will use table1 and table2 to generate a table3 .我正在尝试编写一个 MySQL select 语句,它将使用table1table2生成table3

table1表格1

id ID Month Year Name姓名 Length长度 Breath呼吸
1 1 January一月 2002 2002年 Square正方形 5 5 2 2
2 2 February二月 2003 2003年 Circle圆圈 6 6 3 3
3 3 March行进 2004 2004年 Cylinder圆筒 7 7 4 4
4 4 April四月 2005 2005年 Cube立方体 8 8 5 5
5 5 May可能 2006 2006年 Quadilateral四边形 9 9 6 6

table2表2

id ID Month Year Name姓名 Frequency频率 Area区域 Volume体积
1 1 January一月 2002 2002年 Square正方形 1 1 20 20 50 50
2 2 Febrauy费布劳伊 2003 2003年 Circle圆圈 2 2 25 25 55 55
3 3 March行进 2004 2004年 Cylinder圆筒 3 3 Null空值 60 60
4 4 April四月 2005 2005年 Cube立方体 4 4 35 35 65 65
5 5 May可能 2006 2006年 Quadilateral四边形 5 5 40 40 Null空值

table3 the generated table should be like this. table3 生成的表应该是这样的。

Name姓名 Type类型 Month Year Length长度 Breath呼吸 Frequency频率 Volume/Area体积/面积 Cum_Area Cum_Area Cum_Volume Cum_Volume Area区域 Volume体积
Square正方形 2d 2d January一月 2002 2002年 5 5 2 2 1 1 2.5 2.5 20 20 50 50 20 20 50 50
Circle圆圈 2d 2d February二月 2003 2003年 6 6 3 3 2 2 2.2 2.2 45 45 105 105 25 25 55 55
Cylinder圆筒 3d 3d March行进 2004 2004年 7 7 4 4 3 3 2 2 45 45 165 165 30 30 60 60
Cube立方体 3d 3d April四月 2005 2005年 8 8 5 5 4 4 1.86 1.86 80 80 230 230 35 35 65 65
Quadilateral四边形 4d 4天 May可能 2006 2006年 9 9 6 6 5 5 1.75 1.75 120 120 230 230 40 40 70 70

In the statements I've tried whenever the cumulative column encounters the first null value, every row from then on is null.在累积列遇到第一个空值时我尝试过的语句中,从那时起的每一行都是空值。

Also, how do I write the type column?另外,我如何写类型列? I'd like square and circle to be '2d';我希望正方形和圆形为“2d”; cube and cylinder to be '3d' and Quadrilateral to be '4d'.立方体和圆柱体为“3d”,四边形为“4d”。

Table 1 and 2 have the Name, Month and Year column in common.表 1 和表 2 具有共同的名称、月份和年份列。

I don't finish to understand what is your logic or why you don't have ids on the second table.我还没有完全理解你的逻辑是什么,或者为什么你在第二个表上没有 id。 But assuming what you write, this is a SUGGESTION.但假设你写的是什么,这是一个建议。
Update your question I'll glad to update my answer.更新您的问题我很乐意更新我的答案。

Assuming null means 0:假设 null 意味着 0:

SELECT
table1.Name,
CASE table1.Name 
WHEN Square THEN '2d'
WHEN Circle THEN '2d'
WHEN Cylinder THEN '3d'
WHEN Cube THEN '3d'
WHEN Quadilateral THEN '4d'
END as Type,
table1.Month,
table1.Year,
table1.Length,
table1.Breath,
table2.Frequency,
(IFNULL(table2.Volume,0) / IFNULL(table2.Area,0)) as 'Volume/Area',
//I don't understand what do you need in this field,
//I don't understand what do you need in this field,
table2.Area,
table2.Volume
FROM table1 
JOIN table2
ON table1.Name = table2.Name AND table1.Month = table2.Month AND table1.Year = table2.Year

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM