简体   繁体   English

如何从具有不同(字段x)的表中选择所有字段? 点火枪

[英]How to select all field from table with distinct (field x) ?? Codeigniter

I try this code 我尝试这段代码

function catstore($id)
    {   
        $this->db->distinct();
        $this->db->select('store_id');
        $this->db->from('products');
        //$this->db->join('products','products.store_id = products.store_id');
        $this->db->where('cat_id', $id);
        $result = $this->db->get();
        return $result->result();       
    }

it produces only the distinct store_id but i want all column of the table with distinct rows based on store_id 它只产生不同的store_id,但我希望表的所有列都基于store_id具有不同的行

Suggest me any option 建议我任何选择

Thanks in advance 提前致谢

You can try using group_by 您可以尝试使用group_by

function catstore($id)
{   
    $this->db->select('*');
    $this->db->from('products');
    //$this->db->join('products','products.store_id = products.store_id');
    $this->db->where('cat_id', $id);
    $this->db->group_by('store_id');
    $result = $this->db->get();
    return $result->result();       
}

I know this question is already long time ago, but one may be looking for the same problem like me, and this is how I solved it. 我知道这个问题已经很久了,但是也许有人正在像我一样寻找同样的问题,这就是我解决的方法。

you can omit the select function altogether. 您可以完全省略select功能。 If you are selecting all (*) from a table you do not need to use this function. 如果要从表中选择所有(*),则无需使用此功能。 When omitted, CodeIgniter assumes you wish to SELECT * 省略时,CodeIgniter假定您希望SELECT *

See http://codeigniter.com/user_guide/database/active_record.html#select 请参阅http://codeigniter.com/user_guide/database/active_record.html#select

Or.. 要么..

You can pass comma separated values to select function. 您可以传递逗号分隔的值来select功能。 So pass all column names separated by comma that you want to select 因此,传递您要选择的所有用逗号分隔的列名称

function catstore($id)
    {   
        $this->db->distinct();
        $this->db->select('store_id, column1, column2, cloumn3');
        $this->db->from('products');
        //$this->db->join('products','products.store_id = products.store_id');
        $this->db->where('cat_id', $id);
        $result = $this->db->get();
        return $result->result();       
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM