Cakphpe DISTINCT不使用多个字段

时间:2015-03-05 10:05:14

标签: cakephp

我已写下此查询

pr($this->User->find('all',array('fields'=>array('DISTINCT User.last_name','User.first_name')))); die;

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [last_name] => singh
                    [first_name] => admin
                )

        )

    [1] => Array
        (
            [User] => Array
                (
                    [last_name] => singh
                    [first_name] => jaskaran
                )

        )

)

它不起作用但是当我从字段中删除first_name然后它就是工作

pr($this->User->find('all',array('fields'=>array('DISTINCT User.last_name')))); die;

结果

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [last_name] => singh
                )

        )

)

但我需要两个具有明显的cakephp 2 +

的字段

1 个答案:

答案 0 :(得分:1)

我想你要检索last_name& firstnamelastname的用户表中的DISTINCT

$users = $this->User->find('all', array(
    'fields' => array('User.last_name', 'User.first_name'),
    'group' => array('User.firstname')
));

输出:

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [last_name] => singh
                    [first_name] => admin
                )

        )

)

请查看以下mySQL select one column DISTINCT, with corresponding other columns