11
$q = $this->db->select('
             books.title,
             reserved_books.id,
             reserved_books.isbn, 
             reserved_books.price,
             reserved_books.item_sold,
             reserved_books.date_sold,
             reserved_books.total_amount
            ')
        ->from('reserved_books')
        ->join('books','reserved_books.isbn= books.isbn')
        ->limit($limit,$offset);

我如何在查询中使用 distinct ?reserved_books.isbn 是唯一的。

4

5 回答 5

32

试试下面:

$this->db->distinct();

但 Distinct 并不总是有效。你应该添加 ->group_by("name_of_the_column_which_needs_to_be unique");

$this->db->group_by('column_name');
于 2013-10-22T05:45:10.293 回答
3

你不需要任何加入

 $query=$this->db->distinct()->select('table_attribute')->where('condition')->get('table_name');
  return $query->result();
于 2017-09-16T12:48:26.527 回答
2

在 Select 之前将“DISTINCT”关键字添加到查询中

$this->db->distinct();

参考

于 2013-10-22T05:44:34.310 回答
1

这是一种方法

$select =   array(
             'books.title',
             'reserved_books.id',
             'DISTINCT reserved_books.isbn', 
             'reserved_books.price',
             'reserved_books.item_sold',
             'reserved_books.date_sold',
             'reserved_books.total_amount'
);
$q = $this->db
        ->select($select)
        ->from('reserved_books')
        ->join('books','reserved_books.isbn= books.isbn')
        ->group_by('reserved_books.isbn')
        ->limit($limit,$offset);
于 2013-10-22T05:54:48.217 回答
0

这是删除重复条目的最佳方法。

$query  =  $this->db->select("inf.id, inf.sku, inf.fab_id, inf.sku, inf.amount, inf.min_length, inf.num_of_pieces, inf.width ,inf.type") 
                            ->join("retailcat","retailcat.sku=SUBSTRING(inf.sku,1,19) AND retailcat.category=218","INNER") 
                            ->distinct()
                           ->get("input_factor inf");
于 2015-04-29T10:01:34.050 回答