Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-31T21:29:43.580Z
Answers.LastActivityDate: 2016-07-31T21:29:43.580Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-31T16:40:52.383Z
Answers.Text: Didnt work. I did: $criteria->selectDistinct = 'title'; and $query->selectDistinct = 'title';
Answers.UserDisplayName: user2997606
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-31T06:50:42.363Z
Answers.LastActivityDate: 2016-07-31T06:50:42.363Z
Answers.UserDisplayName: awcross
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-31T14:46:08.633Z
Comments.Text: 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.
Comments.UserDisplayName: Brandon Kelly
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-31T16:40:16.963Z
Comments.Text: Yeah, there is. I only need unique records.
Comments.UserDisplayName: user2997606
Tags: <plugin-development><database><query>
Title: Getting Unique entries?
ThreadText: # 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:
  
  ```php
  $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';  
  
  ---
  
  