mysql将两个日期字段与两个日期字符串进行比较

时间:2013-01-30 09:57:46

标签: mysql sql date

select
start_date,stop_date_original
from dates
where 
start_date is not null
and stop_date_original is not null 
and start_date > str_to_date('10/10/2009','%d/%m/%Y')
/*and stop_date_original < str_to_date('01/24/2013','%d/%m/%Y')*/

此查询工作正常,但是当我取消注释最后一行或使用它来替换结果之前不会受到影响或我得到一个空结果集。

此方法是否存在可能导致此行为的问题?

另外,对于不同的数据库系统,空检查本质上是否必要?

1 个答案:

答案 0 :(得分:2)

停止日期必须为24/01/2013,而非01/24/2013

select
  start_date,
  stop_date_original
from
  dates
where 
  start_date is not null
  and stop_date_original is not null 
  and start_date > str_to_date('10/10/2009','%d/%m/%Y')
  and stop_date_original < str_to_date('24/01/2013','%d/%m/%Y')

或者你必须在函数str_to_date('01/24/2013','%m/%d/%Y')上反转日期和月份。

此外,如果start_date为null,或者stop_date_original为null,则条件将被评估为null,因此您不需要检查它们是否为空,尽管这样做更具可读性。

相关问题