[英]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 语句,它将使用
table1
和table2
生成table3
。
table1表格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 ![]() |
table2表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 the generated table should be like this. 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 ![]() |
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.