Remove duplicate values in a column after join in drupal 7 I am using this db_select query.

$query = db_select('taxonomy_index', 't1');
    $query-&gt;distinct();
    $query-&gt;join('node', 't2', 't1.nid = t2.nid');
    $query-&gt;join('taxonomy_term_data', 't3', 't3.tid = t1.tid');
    $query-&gt;fields('t1', array('tid'));
    $query-&gt;fields('t3', array('name'));
    $query-&gt;fields('t1', array('nid'));
    $query-&gt;groupBy('t1.nid');
    $query-&gt;condition('t2.type', 'article', '=');
    $query-&gt;condition('t3.vid', '2', '=');
    $query-&gt;orderBy('t2.created', 'DESC');
    $query-&gt;range(0, 20);
    $result = $query-&gt;execute()-&gt;fetchAll(); 
    print_r($result);


After running this query 
my output is

tid  name   nid
1    apple   200
5    orange  165
9    grapes  130
5    orange  106


My expecting output is

 tid  name   nid
    1    apple   200
    5    orange  165
    9    grapes  130


No need to repeat duplicate rows like  5 orange 106 again. How this can be possible?
Can you try with the result you are getting and avoid the duplicates using below codes. 

 $used_tids = array();
 foreach ($result as $row) {
        if (!in_array($row-&gt;tid, $used_tids)) {
          $new_result[] = $row;
          $used_tids[] = $row-&gt;tid;
        }
      }
      // new filtered results after avoiding duplicates
      $results = $new_result;