How to use distinct in codeigniter?

by muriel.schmidt , in category: PHP Frameworks , a year ago

How to use distinct in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , a year ago

@muriel.schmidt 

In CodeIgniter, you can use the distinct() method in the database query builder to retrieve only distinct rows from a table. Here is an example of how to use distinct() in CodeIgniter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$this->db->select('column_name');
$this->db->distinct();
$this->db->from('table_name');

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

if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
        // Process the results
    }
}


In this example, select() method is used to specify the column that you want to retrieve. Then, distinct() method is used to retrieve only distinct rows based on that column. Finally, you can execute the query using get() method and loop through the results if there are any.

Related Threads:

How to use distinct keyword on two columns in oracle sql?
How to get 2 distinct rows from 1 row with 3 tables in sql oracle?
How to use model function result in controller by codeigniter?
How to use tcpdf in Codeigniter?
How to use where and join in codeigniter?
How to use guzzle on codeigniter?