Hello.
In the table there is a date field, format: '2016-05-26 15:26:27' how to display the last date value from the table?
I tried to do this:

 " SELECT date FROM tab WHERE MAX(date) = date" " SELECT date FROM tab ORDER BY MAX(date)" " SELECT date FROM tab ORDER BY date" 

    2 answers 2

     SELECT date FROM tab ORDER BY date DESC LIMIT 1 
    • Displays a recent date. - Max
    • @ Max lies ... - Alexey Shimansky
    • quote: a lie ... you judge by yourself? Led not the last value of the date. - Max
    • @ Max means you are doing something wrong ... sqlfiddle.com/#!9/19a730/1 - Alexey Shimansky
    • you are right, you did not clear MAX (date) SELECT MAX(date), date FROM test ORDER BY date DESC LIMIT 1 . And if the table is very large, how productive is this solution? - Max

    Or

     SELECT MAX(date) FROM tab; 

    To output the entire entry (if date is unique)

     SELECT * FROM tab WHERE date IN ( SELECT MAX(date) FROM tab ); -- вместо IN можно использовать использовать = ANY либо = SOME 

    or

     SELECT t1.* FROM tab t1, (SELECT MAX(t2.date) date FROM tab t2) t3 WHERE t1.date = t3.date 
    • The example works: SELECT date FROM tab ORDER BY date DESC Your example is not. - Max
    • @Max SELECT date FROM tab ORDER BY date DESC basically cannot work as required, because returns all records, and one is necessary. As for my code, < sqlfiddle.com / #!9 / 19a730 / 3 / 0 > assumes that it works, moreover, as it should, returning one record, and with the maximum value of the date. - Akina
    • quote: SELECT date FROM tab ORDER BY date DESC basically cannot work as required , I export without a cycle, and my last value is displayed, are you talking about performance? and that it is necessary to add `SELECT date FROM tab ORDER BY date DESC LIMIT 1` Here is the code it does not correct: pole SELECT date, pole FROM tab ORDER BY date DESC LIMIT 1 - Max
    • Here it displays the code not correctly: pole SELECT MAX(date), pole FROM tab; - Max
    • one
      In the initial question about the field, it was not discussed - it is not necessary to change the condition “on the fly”. But if the whole record is needed - use the above code as a subquery in the selection condition (as I understand it, the values ​​in the date field are unique). - Akina