在一个 SQL 查询中使用大于或等于/小于或等于两次

发布于 2024-12-19 02:51:37 字数 543 浏览 3 评论 0原文

我正在尝试创建基本上是时间戳搜索查询的内容。用户输入所需预订的开始时间和结束时间,搜索会找到该时间段内该房间的任何当前预订。如果找到匹配项,用户就会被告知房间已被预订。

我遇到的问题是,当我发送此查询时:

SELECT * FROM event WHERE begintime <= '$start' AND endtime >= '$end' AND room = '$room';

我没有收到任何记录。这些变量保存了正确的信息,所以我怀疑我使用算术运算符的方式是错误的。我环顾四周,发现许多在一个查询中使用两种类型比较的示例,但仅针对一列。我还没有找到任何有人在一个查询中将值与一列进行比较以及将值与另一列进行比较的示例。

有人知道我做错了什么吗?非常感谢任何帮助。谢谢!

编辑:我在纪元时间中设置了时间戳,因此输入搜索的开始时间将大于或等于数据库中已有的时间,介于开始时间和结束时间之间。结束时间则相反,因为输入搜索的时间将小于或等于数据库中的结束时间,落在开始时间和结束时间之间。翻转标志可以在数据库中找到预订之外的时间。输入经过验证。

I'm trying to create what is basically a timestamp search query. The user puts in a beginning time and end time for a desired reservation and the search finds any current reservations for that room during that time period. If a match is found, the user is told that the room is already reserved.

The problem I'm running into is that when I send this query:

SELECT * FROM event WHERE begintime <= '$start' AND endtime >= '$end' AND room = '$room';

I get no records. The variables are holding the right information, so I suspect that the way I'm using the arithmetic operators is wrong. I've looked around plenty and found many examples of both types of comparisons being used in one query, but only for one column. I haven't found any examples of someone comparing a value against one column and a value against another column like this all in one query.

Anyone have any idea what I'm doing wrong? Any help is much appreciated. Thanks!

EDIT: I have the timestamp set in Epoch time, so the start time put into the search will either be greater than or equal to what is already in the database to fall between the start and end time. Inverse goes for the end time, as the time put into the search will either be less than or equal to the end time in the database to fall between the start and end time. Flipping the signs would find times outside the reservation in the database. Inputs are validated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

吐个泡泡 2024-12-26 02:51:37

发现您有不同的场景:

  • 预订在您的搜索期间开始
  • 预订在您结束
    search-period
  • 预订在您 search-period 之前开始并结束
    在它之后。

你必须搜索所有三个

SELECT * FROM event WHERE ((begintime <= '$start' AND endtime >= '$end') OR (begintime >= '$start' AND endtime <= '$start')  OR (begintime >= '$end' AND endtime <= '$end')) AND room = '$room';

To find that you have different scenarios:

  • A reservation starts in your search-period
  • A reservation ends in you
    search-period
  • A reservation starts before you search-period and ends
    after it.

You have to search all three

SELECT * FROM event WHERE ((begintime <= '$start' AND endtime >= '$end') OR (begintime >= '$start' AND endtime <= '$start')  OR (begintime >= '$end' AND endtime <= '$end')) AND room = '$room';
牵强ㄟ 2024-12-26 02:51:37

我猜你应该切换它们?

如下:

SELECT * FROM event WHERE begintime >= '$start' AND endtime <= '$end' AND room = '$room'; 

您需要一个在开始日期之后和结束日期之前的开始时间。
另外 - 您可能需要验证您的开始和结束变量是否符合以下条件:

  1. 它们要么相等(在最坏的情况下)
  2. 开始时间小于结束时间。

否则不保证您会收到任何结果。

You should switch them, I gues?

Here it is:

SELECT * FROM event WHERE begintime >= '$start' AND endtime <= '$end' AND room = '$room'; 

You need a begin time after start date and before end date.
Plus - you may want to validate if your begin and end variables comply to the following:

  1. They are either equal (in worst case)
    OR
  2. begintime is less than endtime.

Otherwise you are not guaranteed to receive any results.

淡写薰衣草的香 2024-12-26 02:51:37

如果我理解这篇文章,$start$end 是用户的输入。

在这种情况下,您应该将查询编写为:

SELECT * FROM event WHERE begintime >= '$start' AND endtime <= '$end' AND room = '$room';

条件按相反顺序排列。

If I understood the post, $start and $end are the inputs from the user.

In that case you should write the query as:

SELECT * FROM event WHERE begintime >= '$start' AND endtime <= '$end' AND room = '$room';

The condtions are put in reverse order.

油饼 2024-12-26 02:51:37
SELECT * 
  FROM event 
 WHERE CASE 
          WHEN begintime > '$start' THEN begintime 
          ELSE '$start' 
       END
       <
       CASE 
          WHEN endtime > '$end' THEN '$end'
          ELSE endtime 
       END
       AND room = '$room';
SELECT * 
  FROM event 
 WHERE CASE 
          WHEN begintime > '$start' THEN begintime 
          ELSE '$start' 
       END
       <
       CASE 
          WHEN endtime > '$end' THEN '$end'
          ELSE endtime 
       END
       AND room = '$room';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文