dongyi1215 2016-07-14 10:37
浏览 63
已采纳

如何从mysql(自定义数据字段)中选择最近7天的结果?

I need to select data from mysql for last 7 days. I have field named 'date' and which have values in mm.dd.yy format. So i tried to find special mysql request to do that, but its not work with my field, i gues that beacause date in wrong format. How i can do that from php (use some variable to get mysql entries), or with custom select query ?

  • 写回答

2条回答 默认 最新

  • dongtanzhu5417 2016-07-14 11:11
    关注

    You can use STR_TO_DATE() to convert your idiosyncratic date format to a standard DATE value. An expression like this will do the trick

     STR_TO_DATE('07.17.97', '%m.%d.%y')
    

    Then you can say

    WHERE STR_TO_DATE(`date`, '%m.%d.%y') >= CURDATE() - INTERVAL 7 DAY
    

    in your query to filter items with date values starting a week ago.

    But, if you have a lot of rows to filter you will have poor performance: this kind of WHERE clause is not sargable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?