如何在cakephp查询中使用group_contact?

时间:2016-04-12 07:45:55

标签: cakephp cakephp-2.0 cakephp-2.3 cakephp-2.1

SELECT GROUP_CONCAT(usertype_name SEPARATOR ','), customer_name FROM `pre`.`customer` AS `Viewcustomer` WHERE `Viewcustomer`.`customer_status` = 0 AND `Viewcustomer`.`customer_type` = 'A1'  
GROUP BY `customer_id`  DESC

如何在cakephp中编写此查询?

请帮我在cakephp中写上面的查询。我不知道如何在cakephp查询中编写group_contact。我正在使用cakephp 2x

2 个答案:

答案 0 :(得分:3)

在CakePHP 3.X

$query = $this->Model->find('all');
$query->select(['field1',
                'field2' => 'group_concat(field2)',
                'field3'])
        ->group('field2');

答案 1 :(得分:1)

试试这个

$this->ModelName->query('SELECT GROUP_CONCAT(usertype_name SEPARATOR ','), customer_name FROM `pre`.`customer` AS `Viewcustomer` WHERE `Viewcustomer`.`customer_status` = 0 AND `Viewcustomer`.`customer_type` = 'A1'
GROUP BY `customer_id`  DESC');

供参考检查cakephp doc http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-query