Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Sql-查找日期是否介于两个日期之间_Mysql_Sql - Fatal编程技术网

Mysql Sql-查找日期是否介于两个日期之间

Mysql Sql-查找日期是否介于两个日期之间,mysql,sql,Mysql,Sql,我有一个字段FromDate和一个字段ToDate 我正在查找今天介于from和to之间的行 select * from job where job.type='manager' and '2014-01-22' between job.FromDate and job.ToDate 查询不会引发异常,甚至会返回一些行。但这是不对的——它返回的行没有我要查找的日期 另外,我使用的日期格式对于我的数据库是正确的。试试这个 select * from job w

我有一个字段FromDate和一个字段ToDate

我正在查找今天介于from和to之间的行

      select * from job
      where job.type='manager'
      and '2014-01-22' between job.FromDate and job.ToDate
查询不会引发异常,甚至会返回一些行。但这是不对的——它返回的行没有我要查找的日期

另外,我使用的日期格式对于我的数据库是正确的。

试试这个

select * from job
where job.type='manager'
and job.FromDate <= '2014-01-22' and job.ToDate >= '2014-01-22'

比较日期通常很棘手,尤其是当值存储为datetime而不是date时。时间成分会影响比较。另一种可能是ToDate对于最近的记录为空。以下是解决此问题的一种方法:

  select *
  from job
  where job.type ='manager' and
        date('2014-01-22') between date(job.FromDate) and date(coalesce(job.ToDate, '2099-12-31'))
但是,对列使用函数可能会降低查询效率。相反,您可以尝试:

  select *
  from job
  where job.type ='manager' and
        job.FromDate < '2014-01-23' and
        (job.ToDate >= '2014-01-22' or job.ToDate is null);

考虑提供适当的DDLS显示创建表语句和上面的插入和/或SqLFIDLE。请显示示例数据、期望的结果和实际获得的结果。如何验证结果不正确?谢谢,但也不管用。有趣的是,它返回与我的查询相同的列表。这是错误的答案。好吧,抱歉你们的麻烦。我做错了什么。