select sql data order by date

不羁的心 提交于 2019-12-02 13:31:50

I'm blindly assuming MySQL here, because it makes sense in the context of this question. Nobody using another database engine would dare create this problem in the first place.

STR_TO_DATE to the rescue! Given the format "March 01, 2000", the following conversion should work.

SELECT STR_TO_DATE(column_name, '%M %d, %Y')
  FROM TableName
 WHERE ...
 ORDER BY STR_TO_DATE(column_name, '%M %d, %Y')

You may need to adjust the format string a bit.

Yes, you can use the DATE function along with CURDATE.

SELECT * FROM tbl WHERE DATE(your_date_column) = CURDATE()

This will select all rows where your_date_column is todays date.

If you want to select all rows where your_date_column is between two dates, you can simply use BETWEEN:

SELET * FROM tbl WHERE your_date_column BETWEEN '20111-03-20' AND '2011-03-30'

If your two dates are in variables such as $date_begin and $date_end, you could use an SQL query like the following one, to get data that's between those dates :

select *
from your_table
where date between '$date_begin' and '$date_end'


(Not sure I quite understand the question)
If you want to get the rows that correspond to the current date, you'll need to variables :

  • $current ; that would be like 2011-03-20 -- which means 2011-03-20 00:00:00
  • $next ; that would be like 2011-03-21 -- which means 2011-03-21 00:00:00

And you'll use a query like this one :

select *
from your_table
where date >= '$current' and date < '$next'

To select data that has its date which is greater or equal that today ; and that's before tomorrow.


In any case, try to not apply an SQL function on your date column ; don't do anything like this :

where some_function(date) >= ...

Doing this, your database engine would have to apply that function to all lines of your table, not using any index you might have on the date column -- and this will result in bad performances.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!