SQL 大于、等于和小于

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9610361/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:43:47  来源:igfitidea点击:

SQL Greater than, Equal to AND Less Than

sqlselect

提问by user1081326

I want to create a query like the following, But im unsure of how to code it correctly, I want it to return all bookings within 1 hour of a StartTime, Here is what i came up with:

我想创建一个如下所示的查询,但我不确定如何正确编码,我希望它在开始时间的 1 小时内返回所有预订,这是我想出的:

SELECT BookingId, StartTime
FROM Booking
WHERE StartTime <=> 1.00

Is the possible? or Is there a way round it?

有可能吗?或者有办法解决吗?

Everything ive found on the web hasn't been about using Greater than, Equal to and Less Than all in the same query.

我在网上找到的所有内容都不是关于在同一个查询中使用大于、等于和小于。

回答by zerkms

Supposing you use sql server:

假设您使用 sql server:

WHERE StartTime BETWEEN DATEADD(HOUR, -1, GetDate())
                    AND DATEADD(HOUR, 1, GetDate())

回答by James Hay

If start time is a datetime type then you can use something like

如果开始时间是日期时间类型,那么您可以使用类似

SELECT BookingId, StartTime
FROM Booking
WHERE StartTime >= '2012-03-08 00:00:00.000' 
AND StartTime <= '2012-03-08 01:00:00.000'

Obviously you would want to use your own values for the times but this should give you everything in that 1 hour period inclusive of both the upper and lower limit.

显然,您希望使用自己的时间值,但这应该为您提供 1 小时内的所有内容,包括上限和下限。

You can use the GETDATE() function to get todays current date.

您可以使用 GETDATE() 函数获取今天的当前日期。

回答by Phil

declare @starttime datetime = '2012-03-07 22:58:00'

SELECT BookingId, StartTime
FROM Booking
WHERE ABS( DATEDIFF( minute, StartTime, @starttime ) ) <= 60

回答by Standage

Somthing like this should workL

像这样的事情应该工作L

SELECT BookingId, StartTime
FROM Booking
WHERE StartTime between dateadd(hour, -1, getdate()) and getdate()