Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch the last row in MySQL in THIS scenario?

Tags:

python

sql

mysql

I have the table - checkdate

The Structure is ...

Curr_date 

 150120
 160120
 170120
 180120

( Date format is DDMMYY )

Only Single column is there which is the 'Primary Key'

Now I want to fetch the LATEST DATE or CURRENT DATE saved in the table ( Which will not always be the last row )

Example: The Date latest added date is : 010122

Then, the record will show like...


Curr_date

010122        <--- The Latest date is on the top
150120
160120
170120


Now how to fetch the Last Added Row in MySQL ? In this Scenario ?

( Note : select TOP 1 * from table_name order by Id desc , doesn't work... And also I want to work without ID column... )

EDITED : The Datatype is String

like image 233
KAKAKAKI Avatar asked Jan 25 '23 09:01

KAKAKAKI


1 Answers

Assuming the data type of curr_date is string. Following query will work for you.

select curr_date
       , STR_TO_DATE(curr_date,'%d%m%Y') 
from test 
order by STR_TO_DATE(curr_date,'%d%m%Y') desc;
like image 161
Naveen Kumar Avatar answered Jan 31 '23 21:01

Naveen Kumar