SELECT * FROM表WHERE日期BETWEEN显示去年的数据,为什么?

时间:2015-09-02 16:18:19

标签: php mysql

此查询在去年有效,但今年它显示了去年的信息。为什么会这样?

$monday_date = 08-31-2015
$friday_date = 09-04-2015

SELECT * FROM cal_notes WHERE date BETWEEN '$monday_date' AND '$friday_date' ORDER by date ASC

去年8月31日至9月4日期间,有一些记录显示了这些记录。今年在这2个日期之间,它显示了今年这两个日期之间的记录以及这两个日期之间去年的记录。如何让它停止显示过去几年的数据?

... UPDATE

以下是我尝试的所有不同的事情,因为我发布了问题并得到了答案。似乎没什么用。没有任何结果我写它。

SELECT * FROM cal_notes WHERE STR_TO_DATE(cal_date, '%m-%d-%Y') BETWEEN '$monday_date' AND '$friday_date'

SELECT * FROM cal_notes WHERE STR_TO_DATE(`cal_date`, '%m-%d-%Y') BETWEEN '$monday_date' AND '$friday_date'

SELECT * FROM cal_notes WHERE STR_TO_DATE(`cal_date`, '%m-%d-%Y') BETWEEN '$monday_date' AND '$friday_date' ORDER by cal_date ASC

SELECT * FROM cal_notes WHERE STR_TO_DATE('cal_date', '%m-%d-%Y') BETWEEN '$monday_date' AND '$friday_date' ORDER by cal_date ASC

SELECT * FROM cal_notes WHERE STR_TO_DATE(cal_date, '%m-%d-%Y') BETWEEN '$monday_date' AND '$friday_date' ORDER by cal_date ASC

SELECT * FROM cal_notes WHERE STR_TO_DATE(`date`, '%m-%d-%Y') BETWEEN '$monday_date' AND '$friday_date' ORDER by date ASC

SELECT * FROM cal_notes WHERE STR_TO_DATE(`date`, '%d,%m,%Y') BETWEEN '$monday_date' AND '$friday_date' ORDER by date ASC

SELECT * FROM `cal_notes` WHERE `cal_date` BETWEEN concat(substr('$monday_date',7,4),'-',substr('$monday_date',1,2),'-',substr('$monday_date',4,2)) AND concat(substr('$friday_date',7,4),'-',substr('$friday_date',1,2),'-',substr('$friday_date',4,2))

SELECT * FROM `cal_notes` WHERE `date` BETWEEN concat(substr('$monday_date',7,4),'-',substr('$monday_date',1,2),'-',substr('$monday_date',4,2)) AND concat(substr('$friday_date',7,4),'-',substr('$friday_date',1,2),'-',substr('$friday_date',4,2))

SELECT * FROM cal_notes WHERE STR_TO_DATE(date, '%Y-%m-%d') BETWEEN '$monday_date' AND '$friday_date' ORDER by date ASC

SELECT * FROM cal_notes WHERE STR_TO_DATE(`date`, '%M %d,%Y') BETWEEN '$monday_date' AND '$friday_date' ORDER by date ASC

SELECT * FROM cal_notes WHERE STR_TO_DATE(`cal_date`, '%d,%m,%Y') BETWEEN '$monday_date' AND '$friday_date' ORDER by cal_date ASC

SELECT * FROM cal_notes WHERE STR_TO_DATE(`cal_date`, '%M %d,%Y') BETWEEN '$monday_date' AND '$friday_date' ORDER by cal_date ASC

SELECT * FROM cal_notes WHERE cal_date BETWEEN '$monday_date' AND '$friday_date' ORDER by cal_date ASC

SELECT * FROM cal_notes WHERE date BETWEEN '$monday_date' AND '$friday_date' ORDER by date ASC

3 个答案:

答案 0 :(得分:2)

如果存储在VARCHAR字段中的日期格式是统一的,您应该可以使用STR_TO_DATE(),如下所示:

WHERE STR_TO_DATE(`date`, '[blahblah]') BETWEEN ....

请注意,将BETWEEN范围界限转换为YYYY-MM-DD配置的格式正确的日期字符串可能会更好,但不是很多。 (这样的查询会很慢,因为它们无法利用索引)。

修改:对于此问题,[blahblah]应该是%m-%d-%Y

答案 1 :(得分:0)

将字段格式更改为DATETIME

答案 2 :(得分:0)

如果日期总是遵循相同的模式(看起来像是脸部),你可以尝试以下方法 - 它不漂亮:

select * from `cal_notes` where `date`
between 
    concat(substr('$monday_date',7,4),'-',substr('$monday_date',1,2),'-',substr('$monday_date',4,2))
    and 
    concat(substr('$friday_date',7,4),'-',substr('$friday_date',1,2),'-',substr('$friday_date',4,2))

或者,您可以在查询之前重新排列php变量吗?我发现str_to_date函数失败了变量的日期格式....