Drupal 7 - 使用 BETWEEN 查询不起作用
我在 Drupal 7 中有一个查询正在搜索自定义表:
$query5 = "SELECT COUNT(reservation_id) as rcount5, reservation_id FROM {reservations} WHERE resource_id = :resource_id AND reservation_date = :reservation_date AND start_time BETWEEN :start_time AND :end_time";
$result5 = db_query($query5, array(':resource_id' => $resource_id, ':reservation_date' => $reservation_date, ':start_time' => $start_time, ':end_time' => $end_time));
该查询不起作用,因为我相信它无法正确识别 BETWEEN 函数并返回 end_time,因为它存在。有没有办法向 drupal 或 db api 显示这是一个 BETWEEN 语句?谢谢。
I have a query in Drupal 7 that is searching a custom table:
$query5 = "SELECT COUNT(reservation_id) as rcount5, reservation_id FROM {reservations} WHERE resource_id = :resource_id AND reservation_date = :reservation_date AND start_time BETWEEN :start_time AND :end_time";
$result5 = db_query($query5, array(':resource_id' => $resource_id, ':reservation_date' => $reservation_date, ':start_time' => $start_time, ':end_time' => $end_time));
The query is not working because I believe that it is not recognizing the BETWEEN function properly and returning the end_time because it exists. Is there a way to show drupal or the db api that this is a BETWEEN statement? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 Drupal 7 数据库 API 并添加一个
BETWEEN
参数,如下所示:在我看来,它更容易阅读:)
看看 动态查询 了解更多信息和示例。
You can use the Drupal 7 database API and add a
BETWEEN
argument like this:It's far easier to read in my opinion :)
Take a look at Dynamic queries for more info and examples.
尝试使用
>=
和<=
代替:Try using
>=
and<=
instead:好吧,想出了答案。首先,我的原始代码运行良好。我必须将数据库中的数据类型更改为 int,之后工作正常。谢谢。
好消息是所有给出的答案都有效。谢谢。
Ok, came up with the answer. First, my original code works fine. I had to change the data type to int in the database and it worked fine after that. Thanks.
The good news is that all of the answers given worked. Thanks.
我完全确定这是默认情况还是 db_query / db_select 中的错误。
假设您使用以下格式将时间戳保存在数据库中
2014-12-15 09:38:04
或者只是
2014-12-15
现在假设您要搜索与某一天相关的所有行。
如果你使用 BETWEEN 抛出一个原始的 MySQL 查询,它将完美地工作。
示例:
但是,如果您使用 db_query 或 db_select 创建查询,则以下语句将不起作用:
示例:
出于某种原因,Drupal 似乎从 2014-12-15 0:0:0 到 2014-12-15 0:0:0 进行比较。
如果您使用以下格式创建条件,它将起作用
。使用真实的过滤器,语句将是:
我希望我对这个 Drupal 问题的看法是错误的。因为实在是太蹩脚了。
问候,
谢里夫。
I'm entirely sure if this is by default or a bug in db_query / db_select.
Let's assume that you save your timestamp in DB with the following format
2014-12-15 09:38:04
or just
2014-12-15
Now lets assume you want to search all rows related to one single day.
If you throw a raw MySQL query using BETWEEN it will work perfectly.
Example:
However, if you create the query using db_query or db_select, the statement below will not work:
Example:
For some reason, it seems Drupal compares from 2014-12-15 0:0:0 to 2014-12-15 0:0:0.
If you create the condition with the following format, it will work
Using real filters, the statement would be:
I'm hoping i'm wrong about this Drupal issue. Because it's really lame.
Regards,
Sharif.
不适用于 drupal,因为它没有真正的过滤器。
@Sharif El Shobkshy 给出的解决方案是可行的解决方案。
请注意,$stardate 和 endate 必须采用 php
date(Yml)
格式。但如果日期存储为整数,请将其更改为date(Yml,$_Post['date'])
并更改实际过滤器。doesn't work on drupal because it doesn't have a real filter.
the solution given by @Sharif El Shobkshy is the working solution.
note the $stardate and endate must be in the php
date(Y-m-l)
format. But if the date is stored as integer, change it todate(Yml,$_Post['date'])
and change the real filter too.如果您在查询中使用
COUNT()
和其他字段选择,则还应该使用GROUP BY
子句If you use
COUNT()
and other field selection in your query, you should also useGROUP BY
clause