web-dev-qa-db-fra.com

comment utiliser DISTINCT ici dans ma requête 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);

comment puis-je utiliser distinct ici dans ma requête? reserved_books.isbn est l'unique.

7
Ezekiel Blanco

Essayez ci-dessous:

$this->db->distinct();

mais Distinct ne fonctionnera pas toujours. Vous devriez ajouter ->group_by("name_of_the_column_which_needs_to_be unique");

$this->db->group_by('column_name');
25
Mandip Darji

Vous n'avez pas besoin de rejoindre

 $query=$this->db->distinct()->select('table_attribute')->where('condition')->get('table_name');
  return $query->result();
2
Zia

Ajoute le mot clé "DISTINCT" à une requête avant Select

$this->db->distinct();

Référence

2
Moeed Farooqui

Voici un moyen de le faire

$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);
1
Muhammad Raheel

Voici le meilleur moyen de supprimer les entrées en double.

$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
Vikas Kumar