Reputation: 49
I have 3 column in a table where there are many rows. for particular row i have to get the column's last value.so how to get the last date that is 11/10/2013.
uid date book_id
1 18/8/2013 41
to
11/10/2013
Upvotes: 0
Views: 414
Reputation: 1240
This should get you going.
SELECT * FROM table_name ORDER BY STR_TO_DATE(date,'%e/%c/%Y') DESC LIMIT 1;
-- ^- the most recent date is first
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
Upvotes: 0
Reputation: 40695
You want the latest date entry? Order it descending (latest date first) and limit the resultset to 1.
SELECT date FROM table ORDER BY date DESC LIMIT 1 ;
In case you really meant that you've got several dates in one date field and even text also, then you need to change your database schema to something like:
uid | startDate | endDate | bookID
Upvotes: 1
Reputation: 468
That's how you will do this in php using mysql
$Query = mysql_query(SELECT date FROM table_name ORDER BY date DESC LIMIT 1);
$Date = mysql_result($Query,0,'date');
$Date = explode('to',$Date);
$Last_Date = $Date[1];
Upvotes: -1
Reputation: 7597
The problem is that you're storing dates as strings, not as dates. 11/10/2013
is not a valid date, and you'll never manage to sort it properly without using DATE_FORMAT or other functions. That's overkill given MySQL's fine native handling of dates.
Also, if you're storing 2 dates, use 2 columns (for example date_start
and date_end
). Never combine them into 1.
Backup your data, change the type of the column to DATE or DATETIME and use ORDER BY date_field DESC LIMIT 1
.
Upvotes: 1
Reputation: 485
If you want the last date for a given book_id, you can write something like
SELECT MAX(date) FROM myTable WHERE book_id = 41
Upvotes: 0