CodeIgniter中的GROUP_CONCAT

时间:2013-11-09 01:28:26

标签: php sql codeigniter group-concat

我只是阻止了如何在CodeIgniter中使用我的sql请求创建我的group_concat。 我的所有查询都使用Jtable库列在表中。

除非我尝试插入GROUP_CONCAT,否则一切正常。

这是我的模特页面:

function list_all()
    {
        $login_id = $this->session->userdata('User_id');
        $this->db->select('p.project_id, p.Project, p.Description, p.Status, p.Thumbnail, t.Template');
        $this->db->from('assigned_projects_ppeople a');
        $this->db->where('people_id', $login_id);
        $this->db->join('projects p', 'p.project_id = a.project_id');
        $this->db->join('project_templates t', 't.template_id = p.template_id');
        $this->db->select('GROUP_CONCAT(u.Asset SEPARATOR ",") as assetslist', FALSE);
        $this->db->from('assigned_assets_pproject b');
        $this->db->join('assets u', 'u.asset_id = b.asset_id');
        $query = $this->db->get();

        $rows = $query->result_array();

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Records'] = $rows;
        return $jTableResult;
    }

我的控制器页面:

function listRecord(){
        $this->load->model('project_model');
        $result = $this->project_model->list_all();
        print json_encode($result);
    }

完成我的观看页面:

<table id="listtable"></table>
        <script type="text/javascript">
            $(document).ready(function () {
            $('#listtable').jtable({
                title: 'Table test',
                actions: {
                    listAction: '<?php echo base_url().'project/listRecord';?>',
                    createAction: '/GettingStarted/CreatePerson',
                    updateAction: '/GettingStarted/UpdatePerson',
                    deleteAction: '/GettingStarted/DeletePerson'
                },
                fields: {
                    project_id: {
                        key: true,
                        list: false
                    },
                    Project: {
                        title: 'Project Name'
                    },
                    Description: {
                        title: 'Description'
                    },
                    Status: {
                        title: 'Status',
                        width: '20px'
                    },
                    Thumbnail: {
                        title: 'Thumbnail',
                        display: function (data) {
                            return '<a href="<?php echo base_url('project');?>/' + data.record.project_id + '"><img class="thumbnail" width="50px" height="50px" src="' + data.record.Thumbnail + '" alt="' + data.record.Thumbnail + '" ></a>';
                        }
                    },
                    Template: {
                        title: 'Template'
                    },
                    Asset: {
                        title: 'Assets'
                    },
                    RecordDate: {
                        title: 'Record date',
                        type: 'date',
                        create: false,
                        edit: false
                    }
                }
            });
            //Load person list from server
            $('#listtable').jtable('load');
        });
        </script>

我阅读了很多关于这个的帖子,比如替换','分隔符“,”或者使用OUTER到连接,或者在使用get方法之前使用group_by('p.project_id'),不起作用。< / p>

以下是json中查询的输出:

{"Result":"OK","Records":[{"project_id":"1","Project":"Adam & Eve : A Famous Story","Description":"The story about Adam & Eve reviewed in 3D Animation movie !","Status":"wip","Thumbnail":"http:\/\/localhost\/assets\/images\/thumb\/projectAdamAndEve.png","Template":"Animation Movie","assetslist":"Apple, Adam, Eve, Garden of Eden"}]} 

我们可以看到GROUP_CONCAT在这里(在“assetslist”之后),但该列仍为空。

如果被问到,我可以发布数据库SQL文件。

2 个答案:

答案 0 :(得分:2)

请你试试这个(编辑):

  

$ this-&gt; db-&gt; select('GROUP_CONCAT(u.Asset)AS assetslist');

答案 1 :(得分:0)

如此简单,在我的鼻子下,但我没有看到它...... GROUP_CONCAT为新的相应列使用别名(作为&#34; assetslist&#34;)。 我不得不使用这个名称,而不是我的非连接表列的默认名称&#34;资产&#34;在Jtable配置中:

<table id="listtable"></table>
        <script type="text/javascript">
            $(document).ready(function () {
            $('#listtable').jtable({
                title: 'Table test',
                actions: {
                    listAction: '<?php echo base_url().'project/listRecord';?>',
                    createAction: '/GettingStarted/CreatePerson',
                    updateAction: '/GettingStarted/UpdatePerson',
                    deleteAction: '/GettingStarted/DeletePerson'
                },
                fields: {
                    project_id: {
                        key: true,
                        list: false
                    },
                    Project: {
                        title: 'Project Name'
                    },
                    Description: {
                        title: 'Description'
                    },
                    Status: {
                        title: 'Status',
                        width: '20px'
                    },
                    Thumbnail: {
                        title: 'Thumbnail',
                        display: function (data) {
                            return '<a href="<?php echo base_url('project');?>/' + data.record.project_id + '"><img class="thumbnail" width="50px" height="50px" src="' + data.record.Thumbnail + '" alt="' + data.record.Thumbnail + '" ></a>';
                        }
                    },
                    Template: {
                        title: 'Template'
                    },
                    assetslist: {
                        title: 'Assets'
                    },
                    RecordDate: {
                        title: 'Record date',
                        type: 'date',
                        create: false,
                        edit: false
                    }
                }
            });
            //Load person list from server
            $('#listtable').jtable('load');
        });
        </script>
谢谢你!