Max date limit for sql by 1

Instead of using a timestamp, I use this one date('Y-m-d h:i:s A')

and store it as a string. My problem is that I cannot get the last value of the date inserted because I am using:

SELECT id, date FROM table Date ORDER_BY DESC limit 1

The output 2015-05-28 09:11:16 PM

is id = 10

fetched which is , but what I am trying to extract is this id = 12

. Is there a way to compare these values ​​and select the last one: id 12?

The table is ordered by date desc

| id  |  date                  |
================================
| 12  | 2015-05-28 09:26:20 PM |
| 11  | 2015-05-28 09:26:04 PM |
| 10  | 2015-05-28 09:11:16 PM |

      

+3


source to share


3 answers


It seems that your date column is not of type datetime, but varchar. You have to convert it to datetime when sorting:

SELECT FROM table
ORDER BY convert(datetime, date, 103) ASC

      



OR with MySQL:

ORDER BY STR_TO_DATE(datetime, '%m/%d/%Y');

      

+5


source


I would ORDER BY id DESC LIMIT 1

This should give you the id in order from earliest to last and give you the current record.



+1


source


In MySQL, you can do this:

SELECT id, date FROM table ORDER BY UNIX_TIMESTAMP(date) DESC LIMIT 1

      

0


source







All Articles