MS SQL Server 查询以检查大厅是否已预订

发布于 2025-01-13 22:29:48 字数 1687 浏览 4 评论 0原文

这个查询让我发疯,非常感谢任何帮助。

我有一个如下表:


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2025-01-20 22:29:48

您可以执行 INSERT...SELECT...WHERE NOT EXISTS,然后检查行数。要比较间隔重叠,请将一个开始时间与另一端时间进行比较,反之亦然。

请注意,您应始终指定插入的列名称,并将值作为参数而不是文本传递。

INSERT INTO Bookings (hallId, startTime, endTime)
SELECT @hallId, @startTime, @endTime
WHERE NOT EXISTS (SELECT 1
    FROM Bookings b WITH (UPDLOCK)  -- needs UPDLOCK to prevent race conditions
    WHERE b.hallId = @hallId
      AND b.startTime < @endTime
      AND b.endTime > @startTime
);

IF @@ROWCOUNT = 0
    THROW 50001, N'Overlapping booking exists', 1;

如果您不想允许两个间隔相邻,您可能需要更改为 <=

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.

Note that you should always specify column names for inserts, and pass values as parameters, not text.

INSERT INTO Bookings (hallId, startTime, endTime)
SELECT @hallId, @startTime, @endTime
WHERE NOT EXISTS (SELECT 1
    FROM Bookings b WITH (UPDLOCK)  -- needs UPDLOCK to prevent race conditions
    WHERE b.hallId = @hallId
      AND b.startTime < @endTime
      AND b.endTime > @startTime
);

IF @@ROWCOUNT = 0
    THROW 50001, N'Overlapping booking exists', 1;

You may want to change to <= if you don't want to allow two intervals to abut.

隐诗 2025-01-20 22:29:48

感谢曾经试图提供帮助的人,我明白了,

有时您需要的只是快速休息一下。

我发现 smallDateTime 数据类型 表现得很疯狂,它在插入表时交换月份和日期,

例如,如果我插入 2022-03-15 20:00:00 code> 它将被记录为 2022-15-03 20:00:00 ,反之亦然

所以这对我有用,它可能会帮助某人近或远的未来

   -- 2022-03-11 13:30:00, 2022-03-11 15:00:00 => sample data 1 in the table 

   -- 2022-03-11 09:30:00, 2022-03-11 12:30:00 => sample data 2 in the table

    
    DECLARE @startTime AS SmallDateTime = '2022-03-11 15:01:00'
    DECLARE @endTime AS SmallDateTime =   '2022-03-11 19:30:00' 
    DECLARE @hallId AS bigInt = 1


    SELECT * FROM Bookings WHERE 
    (startTime BETWEEN @startTime AND @endTime) OR 
    (endTime BETWEEN @startTime AND @endTime) AND hallId=@hallId

Thanks to who ever tried to help, I figured it out,

Sometimes all you need is to take a quick break.

I found out that smallDateTime data type was acting crazy, it was swapping the month and the day while inserting in the table

e.g if I Inserted 2022-03-15 20:00:00 it would be recorded as 2022-15-03 20:00:00 and vice-versa

So Here goes what worked for me, It may help someone in the nearer or far future

   -- 2022-03-11 13:30:00, 2022-03-11 15:00:00 => sample data 1 in the table 

   -- 2022-03-11 09:30:00, 2022-03-11 12:30:00 => sample data 2 in the table

    
    DECLARE @startTime AS SmallDateTime = '2022-03-11 15:01:00'
    DECLARE @endTime AS SmallDateTime =   '2022-03-11 19:30:00' 
    DECLARE @hallId AS bigInt = 1


    SELECT * FROM Bookings WHERE 
    (startTime BETWEEN @startTime AND @endTime) OR 
    (endTime BETWEEN @startTime AND @endTime) AND hallId=@hallId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文