search

Home  >  Q&A  >  body text

MYSQL get maximum value from a specific date

<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>
P粉824889650P粉824889650530 days ago619

reply all(1)I'll reply

  • P粉441076405

    P粉4410764052023-07-24 16:10:12

    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!


    reply
    0
  • Cancelreply