mysql – 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语句?谢谢.
解决方法:
您可以使用Drupal 7数据库API并添加BETWEEN参数,如下所示:
$query5 = db_select('reservations', 'r')
->fields('r', array('reservation_id'))
->condition('resource_id', $resource_id)
->condition('reservation_date', array($start_time, $end_time), 'BETWEEN');
$query5->addExpression('COUNT(reservation_id)', 'rcount5');
$result5 = $query->execute();
在我看来,它更容易阅读:)
查看Dynamic queries以获取更多信息和示例.
标签:mysql,drupal-7,drupal-modules 来源: https://codeday.me/bug/20190610/1209078.html