![](/img/trans.png)
[英]How can I change this query returning multiples records to return a single record containing the concatenation of the original records?
[英]How can I modify this query to add a new field containing the maximum value of a field of a subset of the total original records?
我不太喜歡數據庫,在實現查詢時遇到以下問題。 我正在使用MySql
我有一個像這樣的MeteoForecast表:
CREATE TABLE MeteoForecast (
id BigInt(20) NOT NULL AUTO_INCREMENT,
localization_id BigInt(20) NOT NULL,
seasonal_forecast_id BigInt(20),
meteo_warning_id BigInt(20),
start_date DateTime NOT NULL,
end_date DateTime NOT NULL,
min_temp Float,
max_temp Float,
icon_link VarChar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
PRIMARY KEY (
id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 ROW_FORMAT=DYNAMIC DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
它包含氣象預報信息,如下所示:
id localization_id start_date end_date min_temp max_temp icon_link
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 1 18/09/2017 06:00:00 18/09/2017 12:00:00 15 24 Mostly_Cloudy_Icon.png
2 1 18/09/2017 12:00:00 18/09/2017 18:00:00 15 24 Light_Rain.png
3 1 19/09/2017 06:00:00 19/09/2017 12:00:00 12 22 Mostly_Cloudy_Icon.png
4 1 19/09/2017 12:00:00 19/09/2017 18:00:00 13 16 Mostly_Cloudy_Icon.png
5 1 20/09/2017 06:00:00 20/09/2017 12:00:00 18 26 Light_Rain.png
6 1 20/09/2017 12:00:00 20/09/2017 18:00:00 17 25 Light_Rain.png
因此,正如您在先前的數據集中看到的那樣,每個記錄都有一個開始日期時間和一個結束日期時間。 這是因為我將在特定日期收集更多的預測信息(它基於時間范圍,在示例中,每天的記錄是從06:00 am到12:00,另一條記錄是12:00到18:00 pm) 。
因此,我創建了一個簡單的查詢,該查詢提取了特定范圍內的所有記錄(在本例中為2天):
select * from MeteoForecast
where start_date between '2017-09-18 06:00:00' and '2017-09-20 06:00:00'
order by start_date desc;
我必須以以下方式修改此查詢:
對於前一個查詢檢索到的每個記錄,必須添加一個名為global_max_temp的新字段,該字段是同一天max_temp字段的最大值。
做一個與與start_date值等於19/09/2017 ...的一天相關的記錄相關的示例,這些是我需要獲取的記錄:
id localization_id start_date end_date min_temp max_temp icon_link global_max_temp
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 1 19/09/2017 06:00:00 19/09/2017 12:00:00 12 22 Mostly_Cloudy_Icon.png 22
4 1 19/09/2017 12:00:00 19/09/2017 18:00:00 13 16 Mostly_Cloudy_Icon.png 22
如您所見,最后一個字段(在此模擬中手動插入)是global_max_temp,並且與該天相關的兩條記錄中的值都為22,因為它是與特定日期相關的所有記錄中max_temp字段的最大值。
這是計算以下global_max_temp值的查詢:
select max(max_temp) from MeteoForecast
where start_date = '2017-09-19 06:00:00'
如何將此功能添加到原始查詢中?
你可以嘗試這樣的事情:
SELECT A.*, B.GLOBAL_MAX_TEMP
FROM (
select id, start_date, end_date, min_temp, max_temp
from MeteoForecast
where start_date between '2017-09-18 06:00:00' and '2017-09-20 06:00:00'
) A
INNER JOIN (SELECT date(start_date) AS date_only, MAX(max_temp) AS GLOBAL_MAX_TEMP
FROM MeteoForecast
WHERE start_date BETWEEN '2017-09-18 06:00:00' and '2017-09-20 06:00:00'
GROUP BY date(start_date)
) B ON date(A.start_date) = B.date_only
ORDER by start_date desc;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.