<p>The query returned by buildElementsQuery() is already prepped to fetch unique entries. But it’s possible for two distinct entries to have the same title, which is why it may <em>look</em> like duplicate entries are being returned.</p>

<p>To only fetch entries with unique titles, do this:</p>

<pre class="lang-php prettyprint-override"><code>$query-&gt;group('content.title');
</code></pre>

CC BY-SA 3.0
2016-07-31T21:29:43.580Z
2016-07-31T21:29:43.580Z
Brandon Kelly
<p>The <code>setDistinct</code> method should be passed a <code>boolean</code> value.</p>

<p><a href="http://www.yiiframework.com/doc/api/1.1/CDbCommand#setDistinct-detail" rel="nofollow">http://www.yiiframework.com/doc/api/1.1/CDbCommand#setDistinct-detail</a></p>

<p>If that doesn't work, you may need to use the <code>selectDistinct</code> method to override the <code>SELECT</code> portion of the query. By default, this sets <code>distinct = true</code>.</p>

<p><a href="http://www.yiiframework.com/doc/api/1.1/CDbCommand#selectDistinct-detail" rel="nofollow">http://www.yiiframework.com/doc/api/1.1/CDbCommand#selectDistinct-detail</a></p>

CC BY-SA 3.0
2016-07-31T16:40:52.383Z
Didnt work. I did: $criteria->selectDistinct = 'title'; and $query->selectDistinct = 'title';
user2997606
CC BY-SA 3.0
2016-07-31T06:50:42.363Z
2016-07-31T06:50:42.363Z
awcross
<p>I am querying data using the code below. In the database, there are duplicate entries for 'title', how can I get distinct values?</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'institutions';
$query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
//$query-&gt;setDistinct('title');
$query-&gt;order('title asc');

$queryResults = $query-&gt;queryAll();
$institutions = EntryModel::populateModels($queryResults);

return $institutions;
</code></pre>

CC BY-SA 3.0
2016-07-31T14:46:08.633Z
Are you sure that you don't just have multiple entries with the same title? The query returned by buildElementsQuery() is already prepped to fetch only unique entries.
Brandon Kelly
CC BY-SA 3.0
2016-07-31T16:40:16.963Z
Yeah, there is. I only need unique records.
user2997606
<plugin-development><database><query>
Getting Unique entries?
# Getting Unique entries?

(Asked by: username_0 on 2016-07-31)

I am querying data using the code below. In the database, there are duplicate entries for 'title', how can I get distinct values?
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'institutions';
$query = craft()->elements->buildElementsQuery($criteria);
//$query->setDistinct('title');
$query->order('title asc');

$queryResults = $query->queryAll();
$institutions = EntryModel::populateModels($queryResults);

return $institutions;

**Tags:** plugin-development, database, query
> **Comments:**
> **username_1**: Are you sure that you don't just have multiple entries with the same title? The query returned by buildElementsQuery() is already prepped to fetch only unique entries.  
> **username_0**: Yeah, there is. I only need unique records.  

---

## 2 answers

### Answer by username_1 on 2016-07-31. Score: 2 | Accepted answer

The query returned by buildElementsQuery() is already prepped to fetch unique entries. But it’s possible for two distinct entries to have the same title, which is why it may look like duplicate entries are being returned.
To only fetch entries with unique titles, do this:
$query->group('content.title');


---

### Answer by username_2 on 2016-07-31. Score: 1

The `setDistinct` method should be passed a `boolean` value.

<http://www.yiiframework.com/doc/api/1.1/CDbCommand#setDistinct-detail>

If that doesn't work, you may need to use the `selectDistinct` method to override the `SELECT` portion of the query. By default, this sets `distinct = true`.

<http://www.yiiframework.com/doc/api/1.1/CDbCommand#selectDistinct-detail>

> **Comments:**
> **username_0**: Didnt work. I did: $criteria->selectDistinct = 'title'; and $query->selectDistinct = 'title';  

---

