如何在cakephp查询中加上“DISTINCT”关键字?

时间:2012-09-03 12:15:18

标签: cakephp

我有这样的代码

$this->Customer->setSource('customer');
        $this->Customer->find('first',array('fields'=>" IF(assigned_id.assigned_id is NULL, 
                                             CONCAT(GROUP_CONCAT(DISTINCT(`team_members`.`user_id`)),'=>TEAM')"));

我需要的输出是这个

SELECT  IF(assigned_id.assigned_id is NULL, CONCAT(GROUP_CONCAT(DISTINCT(`team_members`.`user_id`)),'=>TEAM') FROM `ctk_customer` AS `Customer` WHERE 1 = 1 LIMIT 1

但是我得到了这个输出

SELECT DISTINCT IF(assigned_id.assigned_id is NULL, CONCAT(GROUP_CONCAT(DISTINCT(`team_members`.`user_id`)),'=>TEAM') FROM `ctk_customer` AS `Customer` WHERE 1 = 1 LIMIT 1 

2 个答案:

答案 0 :(得分:2)

$this->Customer->setSource('customer');
        $this->Customer->find('first',array('fields'=>" IF(assigned_id.assigned_id is NULL, 
                                             CONCAT(GROUP_CONCAT(distinct(`team_members`.`user_id`)),'=>TEAM')"));

答案 1 :(得分:0)

it looks like你可以这样做:

array(
    'conditions' => array('Model.field' => $thisValue), //array of conditions
    'recursive' => 1, //int
    'fields' => array('Model.field1', 'DISTINCT Model.field2'), //array of field names
    'order' => array('Model.created', 'Model.field3 DESC'), //string or array defining order
    'group' => array('Model.field'), //fields to GROUP BY
    'limit' => n, //int
    'page' => n, //int
    'offset'=>n, //int
    'callbacks' => true //other possible values are false, 'before', 'after'
)