11

我的数据库中有一个开始日期和一个结束日期,还有一个来自表单字段的 $date 变量。我现在正在尝试查询 $date 是数据库中的开始/结束日期或这两者之间的任何日期的所有行。

这与 daysAsSql 如何工作的文档中描述的内容相反。我不知道如何让它工作。以下行不能作为控制器中的查找条件:

'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),

任何帮助是极大的赞赏。这真让我抓狂。

这是完整的查询和相应的 SQL:

$conditions = array(
            'conditions' => array(
            'and' => array(
                '? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
                'Item.title LIKE' => "%$title%",
                'Item.status_id =' => '1'
                )));

        $this->set('items', $this->Item->find('all', $conditions));



WHERE (('2012-10-06' BETWEEN 'Item.date_start' AND 'Item.date_end') AND (`Item`.`title` LIKE '%%') AND (`Item`.`status_id` = 1))
4

6 回答 6

29
$conditions = array(
        'conditions' => array(
        'and' => array(
                        array('Item.date_start <= ' => $date,
                              'Item.date_end >= ' => $date
                             ),
            'Item.title LIKE' => "%$title%",
            'Item.status_id =' => '1'
            )));

尝试上面的代码并询问它是否不适合您。

编辑: 根据@Aryan 的要求,如果我们必须找到在 1 个月内注册的用户:

$start_date = '2013-05-26'; //should be in YYYY-MM-DD format
$this->User->find('all', array('conditions' => array('User.reg_date BETWEEN '.$start_date.' AND DATE_ADD('.$start_date.', INTERVAL 30 DAY)')));
于 2012-08-13T10:13:56.003 回答
4

这是CakePHP BETWEEN查询示例。

我将我的数组定义为变量,然后在我的 CakePHP find 函数调用中使用这些变量:

// just return these two fields
$fields = array('uri', 'page_views');

// use this "between" range
$conditions = array('Event.date BETWEEN ? and ?' => array($start_date, $end_date));

// run the "select between" query
$results = $this->Event->find('all', 
         array('fields'=>$fields, 
               'conditions'=>$conditions));

参考自

于 2017-06-08T11:47:05.207 回答
0

CakePHP 2.x 查询的一般示例

        $_condition = array("TABLENAME.id" => $id, "TABLENAME.user_id" => array_unique($_array), 'date(TABLENAME.created_at) BETWEEN ? AND ?' => array($start_date, $end_date));
        $result_array = $this->TABLENAME->find("all", array(
            'fields' => array("TABLENAME.id", "TABLENAME.user_id", 'TABLENAME.created_at'),
            "conditions" => $_condition,
            "group" => array("TABLENAME.id"), //fields to GROUP BY
            'joins' => array(
                array(
                    'alias' => 'T2',
                    'table' => 'TABLENAME2',
                    'type' => 'LEFT',
                    'conditions' => array('TABLENAME.t_id = TABLENAME2.t_id')
                ),
                array(
                    'alias' => 'T3',
                    'table' => 'TABLENAME3',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'IF(
                           TABLENAME.t3_id > 0,
                                T2.f_id = T3.f_id,
                                TABLENAME.ff_id = T2.ff_id
                            )'
                    )
                ),
            ),
            'recursive' => 0
                )
        );
于 2017-04-14T09:03:00.900 回答
0

这是更有效和更易于理解IN BETWEEN 的查询cakephp 2.x

$testing_log_device_site_name = $testingLogData['TestingLogDevice']['Siteid'];

$conditions = array('TestingLogDevice.dateee BETWEEN ? and ?' => array($start_date, $end_date));

$results = $this->TestingLogDevice->find('all', 
    array(
         'fields'=>array('dateee','timeee','Siteid'), 
         'conditions'=>array($conditions, 'TestingLogDevice.Siteid'=>$testing_log_device_site_name)
    )
);
pr($results);
于 2017-12-30T06:47:37.633 回答
-1
$data=$this->post->find('all')->where([ 'id'=>$id,
'price between'=>$price1,'and'=>$price2])->toArray();

此查询的工作方式如下:

select * from post where id=$id and price between $price1 and $price2;

" -- 'price between'=>$price1 --" 变为 "price between $price1"

于 2018-07-24T21:36:08.613 回答
-1

用这个

       $today = new DateTime( date('Y-m-d'));

       $fiveYearsBack = $today->sub(new DateInterval('P5Y'));
于 2019-03-05T12:37:49.747 回答