添加日期比较时MySQL查询无效

时间:2017-06-29 09:38:51

标签: mysql phpmyadmin

如果删除WHERE子句中的最后一个条件的日期(settlement_Date),查询工作正常 我也尝试了LIKE运算符,BETWEEN Condition但是没有一个能够推导出结果 SETTLEMENT_DATE列是日期时间类型,格式为' yyyy-mm-dd hh:mm:ss' 我也试过

SETTLEMENT_DATE BETWEEN "2010-04-01 00:00:00" AND "2010-04-01 23:59:59"

我希望查询显示特定日期的结果

SELECT date_format(SETTLEMENT_DATE,"%H:%i") AS "TIME",
SUM(CASE WHEN CORPORATE_ID=1328727040 THEN 1 ELSE 0 END ) AS "TOTAL",
SUM(CASE WHEN CORPORATE_ID=1328727040 AND ( QUOTA_CODE=0 OR QUOTA_CODE =1) THEN 1 ELSE 0 END) AS "Non-Tatkal",
SUM(CASE WHEN CORPORATE_ID=1328727040 AND QUOTA_CODE=2 THEN 1 ELSE 0 END) AS "Tatkal"
FROM ticket
WHERE PNR_NUMBER!="0"
AND CURRENT_RES_STATUS NOT IN (9,10,11,16)
AND date(SETTLEMENT_DATE) = str_to_date('01-04-2014', '%Y-%m-%d')
GROUP BY date_format(SETTLEMENT_DATE,"%H:%i")

感谢任何帮助

2 个答案:

答案 0 :(得分:0)

问题在于:

str_to_date('01-04-2014', '%Y-%m-%d')

当你运行它时:

SELECT STR_TO_DATE('01-04-2014', '%Y-%m-%d');

它给出了:

'2001-04-20'

但您的日期在以下范围内:"2010-04-01 00:00:00" AND "2010-04-01 23:59:59"

答案 1 :(得分:-1)

将date()函数应用于列无法使用可能的索引。我通常发现以下类型的条件适用于日期......

SETTLEMENT_DATE >= '2010-04-01' 
AND SETTLEMENT_DATE < '2010-04-02'

这样,您可以单独保留基于日期的字段,并且&gt; =开始日期很明显,但第二天比午夜的第二天少,与您在2010-04-01 11:59相同:59pm但不包括2010-04-02。