简体   繁体   中英

mysql average temperature of every day in a period

I have a table with date and max temperature.

I'm trying to find the average max temperature of each day of the year given a period of years. Ex. The average of tmax for 01-01 in the period of 1980 and 2013.

With the following query I found Tmax ordered as I want, but I can't find a way to group by each day.

$resultat = mysql_query ("SELECT data, Tmax FROM $estacio WHERE data between '1980-01-01' and '2013-12-31' group by day(data),year(data);");

    while($row = mysql_fetch_array($resultat)) { 
      echo '<tr>';
      echo '<td>',$row[0],'</td><td>',$row[1],'</td>';
      echo '</tr>';
    }

This should give you the average max temperatures per day over multiple years. Each row shows the day and month and the average temperature in the given period for that day.

SELECT
  DAY(data),
  MONTH(data),
  AVG(Tmax)
FROM $estacio 
WHERE data BETWEEN '1980-01-01' AND '2013-12-31'
GROUP BY DAY(data), MONTH(data);

For finding the average surely you would need to use the function AVG() .

Use it like

SELECT
DAY(data)
MONTH(data),
  AVG(Tmax)
FROM $estacio 
WHERE data BETWEEN '1980-01-01' AND '2013-12-31'
GROUP BY DAY(data), MONTH(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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