Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.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
Sql 如何在日期之间获取数据_Sql - Fatal编程技术网

Sql 如何在日期之间获取数据

Sql 如何在日期之间获取数据,sql,Sql,我的数据如下表所示: id startdate finishdate 4 2015-09-17 2016-09-28 5 2016-08-17 2016-09-12 现在我需要这个日期的数据-2016-02-02 例如,如果我输入此日期2016-02-02,则仅显示第一个数据 现在,如果我输入日期-2016-08-10,它将显示这两个数据 我试过: select * from customer where startdate >='2016-02-02' or fi

我的数据如下表所示:

id  startdate   finishdate
4   2015-09-17  2016-09-28
5   2016-08-17  2016-09-12
现在我需要这个日期的数据-
2016-02-02

例如,如果我输入此日期
2016-02-02
,则仅显示第一个数据

现在,如果我输入日期-
2016-08-10
,它将显示这两个数据

我试过:

  select * from customer where startdate >='2016-02-02' or finishdate<='2016-02-02'

select*from customer where startdate>='2016-02-02'或finishdate您的逻辑只是向后的:

select c.*
from customer c
where startdate <= '2016-02-02' and finishdate >= '2016-02-02';
选择c*
来自客户c
其中startdate='2016-02-02';

我建议您不要在日期之间使用
。艾伦·伯特兰(Aaron Bertrand)对这是个坏主意的原因做出了很好的解释。

你的逻辑完全是颠倒的:

select c.*
from customer c
where startdate <= '2016-02-02' and finishdate >= '2016-02-02';
选择c*
来自客户c
其中startdate='2016-02-02';

我建议您不要在日期之间使用
。Aaron Bertrand对为什么这是个坏主意给出了很好的解释。

MySQL或MS SQL Server?不要标记未涉及的产品。我删除了不兼容的数据库标记。为您真正使用的数据库添加回标记。MySQL或MS SQL Server?不要标记未涉及的产品。我删除了不兼容的数据库标记。为您真正使用的数据库添加标签。我不确定为什么要将阻止人们使用中间数据库作为答案的一部分。艾伦·伯特兰在解释结尾的评论中提出的许多批评都是完全合理的理由,说明他对中间人的看法是毫无根据的。我不确定为什么要把阻止人们使用中间人作为回答的一部分。亚伦·伯特朗在解释结尾的评论中的许多批评都是完全合理的理由,说明他对中间人的看法是毫无根据的。