Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort table with timestamp field without considering time of day

Tags:

sql

mysql

I have a table in Mysql (firstname,lastname,data1,data2,...) that one field name is MYDATE and type of this field is timestamp. In this field, the date saved as (yyyy-mm-dd mm:ss:ms), and there are many records of this table.

I want write a select query that sort this table with (yyyy-mm-dd) and without considering (mm:ss:ms).

like image 609
user1400 Avatar asked May 08 '11 06:05

user1400


2 Answers

ORDER BY date(mydate)

but it will cause fullscan.

like image 118
zerkms Avatar answered Oct 30 '22 05:10

zerkms


Just cast it to a date in your order by clause:

SELECT columns
FROM some_table
ORDER BY CAST(mydate AS date);
like image 28
mu is too short Avatar answered Oct 30 '22 05:10

mu is too short