phpcodeigniterselectactiverecorddistinct

How to use DISTINCT in a SELECT query built with CodeIgniter's active record methods


$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);

How can I use distinct here in my query? reserved_books.isbn is the unique one.


Solution

  • 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');