How to Write Distinct Queries in CodeIgniter

To run a distinct query in CodeIgniter, you can use the distinct() method of the Query Builder class. Here’s an example:

$this->db->select('DISTINCT column_name');
$this->db->from('table_name');
$query = $this->db->get();
$result = $query->result();

In the above code, column_name is the name of the column you want to select distinct values for, and table_name is the name of the table you want to select from. The select() method is used to specify the columns to be selected, and the distinct() method is used to apply the distinct operator to the specified column.

The from() method is used to specify the table to select from, and the get() method is used to execute the query and return the results as a query object. Finally, the result() method is used to retrieve the result set as an array of objects.

Distinct in Codeigniter Query

$where_array = array(
'email'=>'test@gmail.com',
'status'=>'1'
);
$shop_name = "members";
$limit = 10;
$offset = 0;
$this->db->distinct();
$query = $this->db->get_where($shop_name,$where_array, $limit, $offset);

Using distinct() Method

$this->db->select('Level');
$this->db->distinct();
$query = $this->db->get('Members');

// Generates SQL String
// SELECT DISTINCT `Level` FROM `Members`;

Using DISTINCT Keyword in Select() Method:

$this->db->select('DISTINCT(Level)');
$query = $this->db->get('Members');

// Generates SQL String
// SELECT DISTINCT(Level) FROM `Members`;

distinct() Query in Codeigniter

$this->db->distinct();
$this->db->get('shop');
// Produces: SELECT DISTINCT * FROM shop

Example:

$record = '123';

$this->db->distinct();

$this->db->select('accessid');

$this->db->where('record', $record);

$query = $this->db->get('accesslog');


get Num Rows

$query->num_rows();

Codeigniter Distinct Query: Select Distinct Column

In CodeIgniter, you can use the $this->db->distinct() method to select distinct values from a column in a database table. Here’s an example code that demonstrates how to write a CodeIgniter distinct query:

$this->db->distinct();
$this->db->select('column_name');
$this->db->from('table_name');
$query = $this->db->get();

In this code, we first call the distinct() method to indicate that we want to select only distinct values from the column. We then use the select() method to specify the name of the column we want to select, and we use the from() method to specify the name of the table.

Finally, we use the get() method to execute the query and retrieve the results. The results are stored in the $query variable, which is a CodeIgniter query result object. You can then use the $query object to iterate over the results and display them as needed.

Note that you can also select multiple columns in a distinct query by passing an array of column names to the select() method, like this:

$this->db->distinct();
$this->db->select(array('column_name_1', 'column_name_2'));
$this->db->from('table_name');
$query = $this->db->get();

This will select only distinct pairs of values from the specified columns.

I hope you get an idea about distinct codeigniter.


#codeigniter 

How to Write Distinct Queries in CodeIgniter
1.00 GEEK