简体   繁体   English

Cakephp MYSQL查询:SELECT,其中日期大于

[英]Cakephp MYSQL query: SELECT where date is greater than

I have to run a MYSQL select query where the query returns all values that have the beginning date 10 days after today. 我必须运行一个MYSQL select查询,该查询返回今天之后10天开始日期的所有值。 I have tried these two methods none of which seem to work, where do I need to make a change? 我已经尝试了这两种方法,但似乎都不起作用,我需要在哪里进行更改?

$fix_d_table = TableRegistry::get('fixed_departures');
$f_dates_march = $fix_d_table   ->find("all")
    ->where(['trek_id' => 3])
    ->andWhere(['month(date_from)' => 03])
    ->andWhere(['date_from' >= date("Y-m-d",strtotime("+10 days"))])
    ->order(['date_from'=>'asc'])
    ->select(['date_from', 'date_to', 'seats_available','id'])
    ->toArray();

$fix_d_table = TableRegistry::get('fixed_departures');
$f_dates_march = $fix_d_table   ->find("all")
    ->where(['trek_id' => 3])
    ->andWhere(['month(date_from)' => 03])
    ->andWhere(['date(date_from)' >= date("Y-m-d",strtotime("+10 days"))])
    ->order(['date_from'=>'asc'])
    ->select(['date_from', 'date_to', 'seats_available','id'])
    ->toArray();

Try below one 尝试以下一项

$fix_d_table = TableRegistry::get('fixed_departures'); $f_dates_march
= $fix_d_table   ->find("all")
    ->where(['trek_id' => 3])
    ->andWhere(
        function ($exp) {
            return $exp->or_([
                'date_from <=' => date('Y-m-d', strtotime("+10 days")),
                'date_from >=' => date('Y-m-d')
            ]);
    })
    ->order(['date_from'=>'asc'])
    ->select(['date_from', 'date_to', 'seats_available','id'])
    ->toArray();

should be 应该

'date_from >=' => date("Y-m-d",strtotime("+10 days"))

if you can rely on mysql server date you can use NOW() inside your query 如果可以依靠mysql服务器日期,则可以在查询中使用NOW()

 ->where(function($exp, $q) {
        return $exp->gte($q->func()->dateDiff([
            'date_from ' => 'identifier',
            $q->func()->now() ]),  10);
    });

this will give you the following SQL condition 这将为您提供以下SQL条件

WHERE 
(
  DATEDIFF(date_from , NOW())
)  >= 10 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM