SQL 在sql中的某个日期之前获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40414774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get values before certain date in sql
提问by joan
i have one question about get multi rows before the date getting infection:
我有一个关于在感染日期之前获取多行的问题:
patientID Appt_reason dateofProcedure
1 1/2/2016
1 1/3/2016
1 infectoin 1/4/2016
2 3/1/2016
2 3/3/2016
3 3/4/2016
3 infectoin 3/5/2016
3 3/6/2016
3 3/7/2016
5 2/2/2016
5 infectoin 2/3/2016
5 2/4/2016
I need to get rows like this
我需要得到这样的行
patientID Appt_reason dateofProcedure
1 1/2/2016
1 1/3/2016
1 infectoin 1/4/2016
3 3/4/2016
3 infectoin 3/5/2016
5 2/2/2016
5 infectoin 2/3/2016
anybody know the answer? thanks
有人知道答案吗?谢谢
回答by Dai
SELECT
patientID,
Appt_reason,
dateOfProcedure
FROM
tableName
WHERE
dateOfProcedure < '2016-04-03'
Note I'm using ISO-8601 format which is unambiguous and supported by all modern database systems.
注意我使用的是 ISO-8601 格式,该格式是明确的,所有现代数据库系统都支持。
Note that it's using the <
(less-than) operator instead of <=
(less-than-or-equal to) because in SQL date values can actually be datetimevalues, so '2016-02-01' >= '2016-02-01 01:00'
is actually false, because '2016-02-01'
has a hidden time component equal to midnight, and 1am is after midnight.
请注意,它使用<
(less-than) 运算符而不是<=
(less-than-or-equal to) 因为在 SQL 中日期值实际上可以是日期时间值,所以'2016-02-01' >= '2016-02-01 01:00'
实际上是假的,因为'2016-02-01'
隐藏的时间组件等于午夜,凌晨 1 点是午夜之后。