开发者

sql - BETWEEN timeframes with a weird timestamp

I have a column that is populated 开发者_高级运维with a date format that is useful for another application but not the mysql format for timestamps. Here is what it looks like:

2010.01.28 12:00 ("time" column) instead of the mysql timestamp: 2010-01-28 12:00:00 ("updatetime" column).

I need to do a date specific search between two time periods and for a normal mysql timestamp I would run this:

SELECT * FROM table WHERE updatetime BETWEEN '$dateStart' and '$dateEnd'

but this doesn't work with the "time" column formatted as it is. I would prefer to keep that column formatted as such as a different application requires that date format, so does anyone know a MYSQL way to search BETWEEN two timeperiods with the 2010.01.28 12:00 format? Running from a PHP script


You can either format the times in the same format in PHP, or in MySQL

WHERE updatetime BETWEEN
        date_format('$datestart','%Y.%m.%d %H:%s')
    AND date_format('$dateend','%Y.%m.%d %H:%s')


You might want to try something like this

SELECT * FROM table WHERE STR_TO_DATE(updatetime, "%d.%m.%Y %h:%i") 
BETWEEN '$dateStart' and '$dateEnd'

to convert the string into a date - that is if the values of $dateStart and $dateEnd are already in the correct format for comparison with a sql date

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜