开发者

Mysql wildcard query problem

WHERE `time` BETWEEN 'Jan 1 %' AND 'Jan 8 %' AND ...

Results

Jan 1 00:33:23
Jan 10 08:52:05

How would I avoid the Jan 10 results?

I have tried a few different combination with %, <=, etc.

Any i开发者_开发百科deas?

Thanks


WHERE Time >= '1/1/2011' AND Time < '1/9/2011'

Or, if you want results from any year:

WHERE DATEPART( month, Time ) = 1 AND DATEPART( day, Time ) < 9


Currently with this query you confuse the search engine because "between" is designed for use with numbers or dates, while you use it with strings.

There are 2 solutions to your problem:

1) Convert your "time" field to "date" and store the dates as "01-01-2010 00:00" (This is the most healthy solution as you will make the DB aware that is a date field) 2) Try:

WHERE `time` >= 'Jan 1' AND `time` < 'Jan 2'

The second solution may give strange results as it is comparing 2 strings, not 2 dates.


I can't try it for you here, but why don't you try around with the STR_TO_DATE method? You can change your time field to a real date, and then don't need to do that string compare:

WHERE STR_TO_DATE(`time`,'%d,%m,%Y') BETWEEN
'2010-01-01'
AND
'2010-01-09'

also, see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜