如何在我的查询CODEIGNITER中使用DISTINCT

时间:2013-10-22 05:41:15

标签: php codeigniter distinct

$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是唯一的。

5 个答案:

答案 0 :(得分:25)

尝试下面的内容:

$this->db->distinct();

但是,区别并不总是有效。您应该添加 - >group_by("name_of_the_column_which_needs_to_be unique");

$this->db->group_by('column_name');

答案 1 :(得分:2)

在选择

之前,将“DISTINCT”关键字添加到查询中
$this->db->distinct();

Refrence

答案 2 :(得分:2)

您不需要任何加入

 $query=$this->db->distinct()->select('table_attribute')->where('condition')->get('table_name');
  return $query->result();

答案 3 :(得分: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);

答案 4 :(得分: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");