我的桌子看起来像这样:
match_name match_formatted_date
A vs B 28.10.2014
b vs c 26.10.2014
c vs d 18.10.2014
d vs e 30.09.2014
e vs f 27.09.2014
g vs h 19.09.2014
h vs j 28.08.2014
x vs y 26.08.2014
y vs z 07.08.2014
MySQL的:
SELECT `m`.* FROM `match` `m`
WHERE (m.match_formatted_date BETWEEN "24.10.2014" AND "31.10.2014")
ORDER BY `m`.`match_id` DESC
但是mysql的返回值如下所示:
A vs B 28.10.2014
b vs c 26.10.2014
d vs e 30.09.2014
e vs f 27.09.2014
h vs j 28.08.2014
x vs y 26.08.2014
我只想得到这个:
A vs B 28.10.2014
b vs c 26.10.2014
注意:match_formatted_date是varchar,而不是datetime。是否有问题?
答案 0 :(得分:2)
您需要将列和搜索字符串转换为日期类型:
SELECT `m`.* `match` `m`
WHERE (str_to_date(m.match_formatted_date, '%d.%m.%Y') BETWEEN str_to_date("24.10.2014", '%d.%m.%Y') AND str_to_date("31.10.2014", '%d.%m.%Y'))
ORDER BY `m`.`match_id` DESC
答案 1 :(得分:0)
现在您正在将字符串与BETWEEN
子句进行比较,这基本上只会比较日期的天数。您可以看到,所有返回的行都有24到31之间的天数。
使用STR_TO_DATE
将字符串转换为可与之比较的正确日期值:
SELECT `m`.*
FROM `match` `m`
WHERE (STR_TO_DATE(m.match_formatted_date,'%d.%m.%Y') BETWEEN STR_TO_DATE('24.10.2014','%d.%m.%Y') AND STR_TO_DATE('31.10.2014','%d.%m.%Y')
ORDER BY `m`.`match_id` DESC