A
A
Alexey Nikolaev2015-12-13 19:36:22
MySQL
Alexey Nikolaev, 2015-12-13 19:36:22

How to force CDbCriteria to perform a distinct query on one field, and not on all?

Good evening.
I pull up the data through Active Record, as an object with conditions I use the CDBCriteria object.

$criteria = new CDbCriteria();

$criteria->distinct = true;
$criteria->select = 'id, alias, title';
$criteria->condition = "urlrouting_show <> 0";
$criteria->order = 'title ASC';

return $this->findAll($criteria);

The alias field may be the same for some records, but due to the inclusion of distinct, these records do not appear in the final selection. I want to enable distinct only for the title field. The SQL analog of this would look like this: Now the SQL analog looks different: Is it possible to do this in Active Record?
SELECT DISTINCT(title), id, alias FROM table
SELECT DISTINCT id, alias, title FROM table

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
enchikiben, 2015-12-14
@Heian

$criteria = new CDbCriteria();

$criteria->select = ['id','alias','DISTINCT(title)'];
$criteria->condition = "urlrouting_show <> 0";
$criteria->order = 'title ASC';

return $this->findAll($criteria);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question