简体   繁体   English

在Codeigniter中使用group_by选择('*')

[英]select('*') with group_by in Codeigniter

I am working in codeigniter. 我在codeigniter中工作。 My problem is i want to show all rows having same id. 我的问题是我想显示所有具有相同ID的行。 But when i perform group_by it only outputs one row of that group. 但是当我执行group_by时,它仅输出该组的一行。

below is my model 下面是我的模特

function category_content(){
        $this->db->select('*');
        $this->db->from('category');
        $this->db->group_by('category_id');
        $query = $this->db->get();
        return $query->result_array();
    }

Please help. 请帮忙。

As per the sql properties Group By groups all the matching records and will show you only one. 根据sql属性“ Group By依据”将所有匹配记录分组,并且只会显示一个。 It seems you want to sort them by id. 看来您想按ID对它们进行排序。 Then its better to use order by 然后最好使用order by

See the Example hope you will get to know this.. 请参阅示例,希望您能了解这一点。

    tableA
    _____
    id  name    marks
    --  ----    ---
    1   x       25
    2   y       27
    1   z       30


    SELECT * FROM tableA group by id

OUTPUT:
1   x   25
2   y   27

in your case you should use WHERE clause. 在您的情况下,您应该使用WHERE子句。

    SELECT * FROM tableA WHERE id=1

OUTPUT:
1   x   25
1   z   30


    function category_content(){
            $this->db->select('*');
            $this->db->from('category');
            $this->db->where($category_id);
            $query = $this->db->get();
            return $query->result_array();
        }

https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

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

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