how to use DISTINCT here in my query CODEIGNITER

前端 未结 5 1146
野趣味
野趣味 2020-12-20 13:40
$q = $this->db->select(\'
             books.title,
             reserved_books.id,
             reserved_books.isbn, 
             reserved_books.price,
              


        
相关标签:
5条回答
  • 2020-12-20 14:28

    Here is a way to do this

    $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);
    
    0 讨论(0)
  • 2020-12-20 14:31

    Try Below:

    $this->db->distinct();
    

    but Distinct will not always work. You should add ->group_by("name_of_the_column_which_needs_to_be unique");

    $this->db->group_by('column_name');
    
    0 讨论(0)
  • 2020-12-20 14:33

    Here is best way to remove duplicate entry.

    $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");
    
    0 讨论(0)
  • 2020-12-20 14:40

    Adds the "DISTINCT" keyword to a query before Select

    $this->db->distinct();
    

    Refrence

    0 讨论(0)
  • 2020-12-20 14:43

    You don't need any join

     $query=$this->db->distinct()->select('table_attribute')->where('condition')->get('table_name');
      return $query->result();
    
    0 讨论(0)
提交回复
热议问题