select sql data order by date

后端 未结 3 1799
我寻月下人不归
我寻月下人不归 2021-01-26 17:09

I have a table with a column called date, as a varchar. The contents are specifically formatted like \'March 11, 2011\'.

How can I select the results have

3条回答
  •  长情又很酷
    2021-01-26 17:16

    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.

提交回复
热议问题