gpt4 book ai didi

php - cakephp 3 的 Between 子句

转载 作者:行者123 更新时间:2023-11-29 07:15:00 26 4
gpt4 key购买 nike

select * from user where HOUR(NOW()) BETWEEN earliest_login AND latest_login

earliest_loginlatest_login 是两列。

请告诉我如何在 cakephp3 中编写此内容

我尝试过的代码是:

$query->where(
[
function($exp) {
return $exp->between('HOUR(NOW())', 'earliest_login', 'latest_login');
}

它不工作。

如果有人有解决方案,请告诉我。

最佳答案

字段名称用于表达式中,SQL 片段不应包含不受信任的内容。

您可以使用:

$query = $users->find()
->where(function ($exp, $q) {
return $exp->between('Users.created', date1, date2);
})

这将创建类似于类似的sql:

select * from users  WHERE created BETWEEN '$date1' AND '$date2'

您还可以在查询中使用 gt() 和 lt()

$query = $articles->find()
->where(function ($exp) {
return $exp
->gt('created', '2016-07-05 12:02:15')
->lt('created', '2016-07-10 12:02:15');
});

如果想要两个日期之间的差异

$query = $this->Users->find();

$diff= $query->func()->dateDiff(['2016-07-04 12:02:15', '2016-07-07 18:29:57']);

$query->select(['difference' => $diff,]);

欲了解更多信息,请阅读此链接 http://book.cakephp.org/3.0/en/orm/query-builder.html

关于php - cakephp 3 的 Between 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38262183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com