Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用CakePhp编写SQL语句(如果为空,则按分组)_Php_Cakephp - Fatal编程技术网

用CakePhp编写SQL语句(如果为空,则按分组)

用CakePhp编写SQL语句(如果为空,则按分组),php,cakephp,Php,Cakephp,因此,我需要使用CakePhp-ORM编写SQL语句,但是如果为NULL(条件),我就无法在CakePhp中编写groupby 这是我的CakePhp查询 $query = TableRegistry::get('Items') ->find('all') ->innerJoinWith('Orders', function ($q) { return $q->where(['Orders.id' => 'Items.order_id']); })

因此,我需要使用CakePhp-ORM编写SQL语句,但是如果为NULL(条件),我就无法在CakePhp
中编写groupby

这是我的CakePhp查询

$query = TableRegistry::get('Items')
  ->find('all')
  ->innerJoinWith('Orders', function ($q) {
    return $q->where(['Orders.id' => 'Items.order_id']);
  })
  ->Where([
    'Items.type' => 1,
    'Items.status' => 50,
  ])
  ->orWhere([
    'Items.type IN ' => [2, 4],
    'Items.status' => 60,
  ])
  ->andWhere([
    'Items.date_of_completion IS NOT' => NULL
  ]);

$query->select([
  'count' => $query->func()->count('*')
]);
谢谢大家!

尝试使用->ifnull()


使用它并享受它:D

问题是到底是什么?@ndm如何从CakePhp$query中的SQL语句中使用GROUP BY IFNULL(i.Vesser\u change\u identifier,i.id)
$query = TableRegistry::get('Items')
  ->find('all')
  ->innerJoinWith('Orders', function ($q) {
    return $q->where(['Orders.id' => 'Items.order_id']);
  })
  ->Where([
    'Items.type' => 1,
    'Items.status' => 50,
  ])
  ->orWhere([
    'Items.type IN ' => [2, 4],
    'Items.status' => 60,
  ])
  ->andWhere([
    'Items.date_of_completion IS NOT' => NULL
  ]);

$query->select([
  'count' => $query->func()->count('*')
]);
$query = TableRegistry::get('Items')
      ->find()
      ->innerJoinWith('Orders')
      ->where([
        'Items.type' => 1,
        'Items.status' => 50,
      ])
      ->orWhere([
        'Items.type IN ' => [2, 4],
        'Items.status' => 60,
      ])
      ->andWhere([
        'Items.date_of_completion IS NOT' => NULL
      ]);
    $query
      ->group(
        $query
          ->func()
          ->ifnull([
            'Items.vessel_change_identifier',
            'Items.id'
          ])
      );
    $query->select([
      'count' => $query->func()->count('*')
    ]);