php 仅从 codeigniter 的列中选择唯一值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17079243/
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
Select only unique values from a column in codeigniter
提问by sharif2008
i have a table like this
我有一张这样的桌子
name|subjects|location
......................
BUET|CSE|Dhaka
BUET|EEE|Dhaka
RUET|CE |Rajshahi
RU |CE |Rajshahi
here all the rows are distinct.And if I use
这里所有的行都是不同的。如果我使用
$this->db->select('*') and $this->db->distinct()
it would select all the rows of BUET but i only want like this
它会选择 BUET 的所有行,但我只想要这样
name|subjects|location
......................
BUET|CSE|Dhaka
RUET|CE |Rajshahi
RU |CE |Rajshahi
That means only the first column must be distinct and select all the columns as usual.And it would work if i use $this->db->select('name') and $this->db->distinct()
. Then what would be about the other columns??
这意味着只有第一列必须是不同的,并像往常一样选择所有列。如果我使用 $this->db->select('name') 和$this->db->distinct()
. 那么其他列会怎样?
As my original table has many columns so I want to use $this->db->select('*')
. I think $this->db->distinct()
does not take any column as parameter. It differentiate result based on select. How can I do this?
由于我的原始表有很多列,所以我想使用$this->db->select('*')
. 我认为 $this->db->distinct()
不以任何列作为参数。它根据选择区分结果。我怎样才能做到这一点?
回答by Gautam3164
Try like this
像这样尝试
$this->db->select('DISTINCT `name`'); //You may use $this->db->distinct('name');
$this->db->select('*');
Select the distinct values by names.And your SELECT spelt wrong,may be its a typing mistake.And you can also use GROUP BY like
按名称选择不同的值。您的 SELECT 拼写错误,可能是打字错误。您也可以使用 GROUP BY 之类的
$this->db->select('*');
$this->db->group_by('name');
回答by jawahar sharma
$this->db->select('*');
$this->db->group_by('column_name');
$this->db->from('tbl_name');
$query = $this->db->get();
return $query->result();
try this
尝试这个
回答by Amit Vikram
You should use group_by in place of distinct.
您应该使用 group_by 代替 distinct。
because distinct will return unique rows. but to have unique column by row you should do group by.
因为 distinct 将返回唯一的行。但是要按行具有唯一的列,您应该进行分组。
Hope this helps.
希望这可以帮助。
回答by Indrasinh Bihola
$this->db->select('column names');
$this->db->distinct();
$query = $this->db->get('table name');
$query->result();
回答by Ahmed Mahmoud
$this->db->select('job,id');
$this->db->group_by('job');
$query= $this->db->get('technicals')->result_array();
if(!empty($query)){
return $query;
}else{
return false;
}