php 如何在我的查询 CODEIGNITER 中使用 DISTINCT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19509805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:36:22  来源:igfitidea点击:

how to use DISTINCT here in my query CODEIGNITER

phpcodeigniterdistinct

提问by Ezekiel Blanco

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

我如何在我的查询中使用 distinct ?reserved_books.isbn 是独一无二的。

回答by Mandip Darji

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

但 Distinct 并不总是有效。你应该添加 ->group_by("name_of_the_column_which_needs_to_be unique");

$this->db->group_by('column_name');

回答by Zia

You don't need any join

你不需要任何加入

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

回答by Moeed Farooqui

Adds the "DISTINCT" keyword to a query before Select

在 Select 之前将“DISTINCT”关键字添加到查询中

$this->db->distinct();

Refrence

参考

回答by Muhammad Raheel

Here is a way to do this

这是一种方法来做到这一点

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

回答by Vikas Kumar

Here is best way to remove duplicate entry.

这是删除重复条目的最佳方法。

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