-2

I have write down this query

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
                )

        )

)

It is not working but when i remove first_name from fields then it is work

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

result

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

        )

)

but i need both field with distinct cakephp 2+

Jaskaran Zap
  • 277
  • 1
  • 2
  • 9
  • What do you expect to happen? That looks like the results of a `DISTINCT` – AgRizzo Mar 05 '15 at 12:06
  • I have mention there what i want, When i pass 2 fields in array then Distinct not working – Jaskaran Zap Mar 05 '15 at 17:42
  • What do you want your final result to look like? You are not clearly explaining what you want: Put in your question what you want the final result to look like. – AgRizzo Mar 05 '15 at 18:11

1 Answers1

2

I think you want to retrieve last_name & firstname from user table where lastname is DISTINCT.

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

Output :

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

        )

)

Just look the following mySQL select one column DISTINCT, with corresponding other columns

Community
  • 1
  • 1
Supravat Mondal
  • 2,574
  • 2
  • 22
  • 33