Cakephp 3区分大小写查询

时间:2016-06-02 08:08:42

标签: mysql date cakephp cakephp-3.0 case-sensitive

    $date = date("Y-m-d");
    $prev_date = date('m-d-Y', strtotime($date .' -1 day'));
    $q = "SELECT `id`, `external_id`, `status`, DATE_FORMAT(from_unixtime(`created`),'%m-%d-%Y') as crt, `extras` 
            FROM `gshuplogs` WHERE DATE_FORMAT(from_unixtime(`created`),'%m-%d-%Y') = '$prev_date' and
            (BINARY `status` in ('success','DEFERRED') or `status` IS NULL)";
    $conn = \Cake\Datasource\ConnectionManager::get('default');
    $res = $conn->execute($q)->fetchAll('assoc');
    debug($res);exit;

我在mysql中编写的上述查询现在我只想在cakephp 3中编写。

我试过了

$res = $this->Gshuplogs->find()->select([
        'id', 'external_id', 'status', 'created'
    ])->where([
        "DATE_FORMAT(from_unixtime(`created`),'%m-%d-%Y')" => $prev_date,
        'OR' => [
            ['LOWER status' => 'success'],
            ['status' => 'DEFERRED'],
            ['status IS NULL']
        ]
    ]);

AND

$res = $this->Gshuplogs->find()->select([
        'id', 'external_id', 'status', 'created'
    ])->where([
        "DATE_FORMAT(from_unixtime(`created`),'%m-%d-%Y')" => $prev_date,
        'OR' => [
            ['BINARY status' => 'success'],
            ['status' => 'DEFERRED'],
            ['status IS NULL']
        ]
    ]);

但这是错误。

  

错误:SQLSTATE [42000]:语法错误或访问冲突:1064您   您的SQL语法有错误;检查对应的手册   您的MySQL服务器版本,以便在附近使用正确的语法   ' `),'%m-%d-%Y'`)=' 06-01-2016' AND('LOWER`状态'成功' OR`状态`=   '防卫厅'在第1行

1 个答案:

答案 0 :(得分:0)

感谢您的回复。我得到了解决方案。

    $date = date("Y-m-d");
    $prev_date = date('m-d-Y', strtotime($date .' -1 day'));

    $res = $this->Gshuplogs->find()->select([
        'id', 'external_id', 'status', "created" 
    ])->where([
        "FROM_UNIXTIME(`created`,'%m-%d-%Y')" => $prev_date,
        'OR' => [
            ['BINARY(status) in ' => ['success','DEFERRED']],
            ['status IS NULL']
        ]
    ]);

    debug($res->toArray());exit;