MS SQL Server 查询以检查大厅是否已预订
这个查询让我发疯,非常感谢任何帮助。
我有一个如下表:
CREATE TABLE Bookings
(
Id bigInt IDENTITY(1,1),
hallId bigInt,
startTime smallDateTime,
endTime smallDateTime
)
INSERT INTO Bookings
VALUES (1, '2022-10-03 08:00:00', '2022-10-03 10:00:00')
--Edit( One of my trials so far
DECLARE @startTime AS SmallDateTime = '2022-10-03 08:00:00'
DECLARE @endTime AS SmallDateTime = '2022-10-03 10:0:00'
DECLARE @hallId AS bigInt = 1
SELECT * FROM Bookings WHERE
startTime >= FORMAT(@startTime, 'yyyy-dd-MM HH:mm:ss') OR startTime <= FORMAT(@endTime, 'yyyy-dd-MM HH:mm:ss') AND
endTime >= FORMAT(@startTime, 'yyyy-dd-MM HH:mm:ss') OR endtime <= FORMAT(@endTime, 'yyyy-dd-MM HH:mm:ss')
AND hallId=@hallId
-- Also this Way
DECLARE @startTime AS SmallDateTime = '2022-03-10 08:00:00'
DECLARE @endTime AS SmallDateTime = '2022-03-10 10:0:00'
DECLARE @hallId AS bigInt = 1
SELECT * FROM Bookings WHERE
startTime >= @startTime OR startTime <= @endTime
AND
endTime >= @startTime OR endtime <= @endTime
AND hallId=@hallId
--Edit)
我的日期时间格式是 'yyyy-MM-dd HH:mm:ss' 但它的记录是以 yyyy-dd-MM HH:mm:ss 格式输入的
所以我想要的是一个检查的查询2 个日期时间范围(例如 2022-10-03 08:00:00 - 2022-10-03 10:00:00),如果有匹配则返回数据,否则不返回任何内容。
我的目标是检查 startTime 或 endTime 是否属于现有预订范围(例如,startTime 或 endTime 都不能在“2022-10-03 08:00:00”至“2022-10-03 10”内) :00:00'),如果找到,则预订不能提前,除非他/她必须更改。我尝试过,但似乎以一种方式工作,而以另一种方式失败
如果有人可以单独签入,即显示 startTime 和 endTime 的特定消息/状态(我的意思是,如果 startTime 对应于现有的 Booking msg='选择不同的开始时间'并且如果结束时间对应于现有的预订消息='选择不同的结束时间'
我还尝试检查开始时间和结束时间的差异是否小于 1 小时
BWT,我正在使用 SQL Server 2014。表达
This query is driving me crazy, any help is appreciated.
I have a table as follows:
CREATE TABLE Bookings
(
Id bigInt IDENTITY(1,1),
hallId bigInt,
startTime smallDateTime,
endTime smallDateTime
)
INSERT INTO Bookings
VALUES (1, '2022-10-03 08:00:00', '2022-10-03 10:00:00')
--Edit( One of my trials so far
DECLARE @startTime AS SmallDateTime = '2022-10-03 08:00:00'
DECLARE @endTime AS SmallDateTime = '2022-10-03 10:0:00'
DECLARE @hallId AS bigInt = 1
SELECT * FROM Bookings WHERE
startTime >= FORMAT(@startTime, 'yyyy-dd-MM HH:mm:ss') OR startTime <= FORMAT(@endTime, 'yyyy-dd-MM HH:mm:ss') AND
endTime >= FORMAT(@startTime, 'yyyy-dd-MM HH:mm:ss') OR endtime <= FORMAT(@endTime, 'yyyy-dd-MM HH:mm:ss')
AND hallId=@hallId
-- Also this Way
DECLARE @startTime AS SmallDateTime = '2022-03-10 08:00:00'
DECLARE @endTime AS SmallDateTime = '2022-03-10 10:0:00'
DECLARE @hallId AS bigInt = 1
SELECT * FROM Bookings WHERE
startTime >= @startTime OR startTime <= @endTime
AND
endTime >= @startTime OR endtime <= @endTime
AND hallId=@hallId
--Edit)
My Datetime format is 'yyyy-MM-dd HH:mm:ss' but it's records are entered in yyyy-dd-MM HH:mm:ss format
So what I want is a query that checks for 2 datetime ranges (say 2022-10-03 08:00:00 - 2022-10-03 10:00:00), if there is a match it returns the data otherwise it returns nothing.
My goal is to check if either of the startTime OR endTime falls within an existing Booking (e.g None of startTime or endTime can't be within '2022-10-03 08:00:00' up to '2022-10-03 10:00:00'), if found, the booking can't precede unless he/she has to alter. I tried between, but seems to work one way and fails another
It would be a plus if someone could check in separately that is to display a specific message/status for startTime and endTime (I mean if the startTime corresponds to existing Booking msg='Select a different start time' and if The endTime corresponds to existing Booking msg='Select a different end time'
Also I tried to check if the difference of startTime and endTime is less then 1 hour.
BWT, I'm using SQL Server 2014 Express
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行 INSERT...SELECT...WHERE NOT EXISTS,然后检查行数。要比较间隔重叠,请将一个开始时间与另一端时间进行比较,反之亦然。
如果您不想允许两个间隔相邻,您可能需要更改为
<=
。You can do
INSERT...SELECT...WHERE NOT EXISTS
, then check the rowcount afterwards. To compare for interval overlap, compare one start time with the other end, and the reverse.You may want to change to
<=
if you don't want to allow two intervals to abut.感谢曾经试图提供帮助的人,我明白了,
我发现
smallDateTime 数据类型
表现得很疯狂,它在插入表时交换月份和日期,例如,如果我插入
2022-03-15 20:00:00
code> 它将被记录为2022-15-03 20:00:00
,反之亦然所以这对我有用,它可能会帮助某人近或远的未来
Thanks to who ever tried to help, I figured it out,
I found out that
smallDateTime data type
was acting crazy, it was swapping the month and the day while inserting in the tablee.g if I Inserted
2022-03-15 20:00:00
it would be recorded as2022-15-03 20:00:00
and vice-versaSo Here goes what worked for me, It may help someone in the nearer or far future