用CakePhp编写SQL语句(如果为空,则按分组)
因此,我需要使用CakePhp-ORM编写SQL语句,但是如果为NULL(条件),我就无法在CakePhp用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']); })
中编写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('*')
]);