开发者

Get MySQL records created in a time range?

开发者 https://www.devze.com 2023-01-28 23:35 出处:网络
I know this is obvious, but my date time row is in TEXT type, and i can\'t figure out how to fetch records that are:

I know this is obvious, but my date time row is in TEXT type, and i can't figure out how to fetch records that are:

  • Inserted since a date until the present

    -- OR --

  • Inserted in a specific day

Some of the failed attempts:

SELECT * FROM posts WHERE DATE(date)=DATE(NOW()) // To get today's posts
SELECT * FROM posts WHERE date>=CURDATE() // Still doesn't work.

P.S. The date row is in a DateTime format but in a TEXT type.

T开发者_如何学运维hanks


The function to parse strings into dates is STR_TO_DATE():

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

It assumes that the format is fixed and known. I don't know if that's the case.


Use convert -- http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html

SELECT * FROM posts WHERE convert(date, datetime) >= CURDATE() 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号