Ordering a MySQL query result based on date
I have a bunch of entries in a database, which I want to sort via date, but the date format looks like this Y-m-d g:i:s
, how could I search the database via the dat开发者_如何转开发e, and not date time?
If it's a datetime field do this for ordering
ORDER BY date_field ASC
For searching do
WHERE DATE(date_field) = '2010-05-08'
If it's a varchar type field try looking at STR_TO_DATE
(link)
select * form table where date(datetime) = '2010-05-08'
That's easy. Convert your dates into proper format first, which is Y-m-d h:i:s
here is an example
$sql="SELECT cast(".$date." AS datetime) as sortdate FROM table order by sortdate ASC";
or
$sql="SELECT cast(DateColumnName AS datetime) as sortdate FROM table order by sortdate ASC";
You can do:
ORDER BY UNIX_TIMESTAMP(your_date)
your_date date needs to be converted first to Y-m-d h:i:s
精彩评论