MYSQL get maximum value from a specific date
P粉824889650
2023-07-23 17:50:20
<p><p style="white-space:normal;">I have a table with years of temperature measurements. </p><p style="white-space:normal;">Looks for the highest measured temperature, including the date of recording, from the current year only. </p><p style="white-space:normal;">Try:</p><p style="white-space:normal;"><br />< /p></p><p><br /></p>
<pre class="brush:php;toolbar:false;">SELECT timestamp, temperature AS max_temp
FROM table
WHERE
temperature = (SELECT max(temperature) FROM table
WHERE timestamp >= '2023-01-01');</pre>
<p>But now the records returned from the complete table show the highest measured temperature in 2023. </p><p>Who has the right way to go?</p><p><br /></p>
You can use the Year() function to extract the year part of the timestamp: Therefore, the modified query will be:
SELECT timestamp (timestamp) = max( of the table Year(CURDATE()) temperature);
Hope it works!