在一个 SQL 查询中使用大于或等于/小于或等于两次
我正在尝试创建基本上是时间戳搜索查询的内容。用户输入所需预订的开始时间和结束时间,搜索会找到该时间段内该房间的任何当前预订。如果找到匹配项,用户就会被告知房间已被预订。
我遇到的问题是,当我发送此查询时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
发现您有不同的场景:
search-period
在它之后。
你必须搜索所有三个
To find that you have different scenarios:
search-period
after it.
You have to search all three
我猜你应该切换它们?
如下:
您需要一个在开始日期之后和结束日期之前的开始时间。
另外 - 您可能需要验证您的开始和结束变量是否符合以下条件:
或
否则不保证您会收到任何结果。
You should switch them, I gues?
Here it is:
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:
OR
Otherwise you are not guaranteed to receive any results.
如果我理解这篇文章,
$start
和$end
是用户的输入。在这种情况下,您应该将查询编写为:
条件按相反顺序排列。
If I understood the post,
$start
and$end
are the inputs from the user.In that case you should write the query as:
The condtions are put in reverse order.