Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use DISTINCT here in my query CODEIGNITER

$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.

like image 452
Ezekiel Blanco Avatar asked Oct 22 '13 05:10

Ezekiel Blanco


People also ask

How use distinct in join query in Codeigniter?

You can use below mentioned query. $query = $this->db->group_by('category_master. Category_Id,business_profile_details. Business_Id');

How do you SELECT distinct data?

The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

How do you use distinct in eloquent?

The LARAVEL ELOQUENT query would be. Meta::distinct()->whereNotNull('meta_value')->get(['meta_value']); 4. A more advanced version of the DISTINCT query would be one where you would want to fetch data from separate columns while still maintaining a value from the column to be distinct.

Where can we use distinct keyword give an example for the same?

The SQL DISTINCT keyword is used in conjunction with the SELECT statement to eliminate all the duplicate records and fetching only unique records. There may be a situation when you have multiple duplicate records in a table.


1 Answers

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');
like image 141
Mandip Darji Avatar answered Sep 28 '22 15:09

Mandip Darji