Domanda

I have some problem with a table that I want to build. This table use a mysql database with mutliple tables linked by many-to-many tables.

I use JSON code to insert values in the jQuery Table.

Here is the model used to query the values in database :

function list_all()
    {
        $login_id = $this->session->userdata('User_id');
        $this->db->select('p.project_id, p.Project, p.Description, p.Status, p.Thumbnail, p.StartDate, p.EndDate, t.template_id, t.Template')
    ->select('GROUP_CONCAT(DISTINCT v.Name SEPARATOR ",") as PeopleList, GROUP_CONCAT(DISTINCT w.asset_id SEPARATOR ",") as AssetsList', FALSE)
    ->from('projects p')
    ->join('assigned_projects_ppeople a', 'a.project_id = p.project_id')
    ->join('assigned_assets_pproject w', 'w.project_id = p.project_id', 'left')
    ->join('project_templates t', 't.template_id = p.template_id')
    ->join('people v', 'v.people_id = a.people_id')
    ->where('a.people_id', $login_id)
    ->group_by('p.project_id');

    $query = $this->db->get();

        $rows = $query->result_array();

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

All values are well listed in the jQuery table, except GROUP_CONCAT values which are duplicated for People Name by the number of assets listed in Assets column... EDIT : Problem resolved using DISTINCT keyword in GROUP_CONCAT.

But, if there's no entry assets linked to a project, the project doesn't be loaded and doesn't appear in table. EDIT : Resolved using LEFT JOIN on "assigned_assets_pproject" table.

Here is the screenshot of the table with JSON code http://i.stack.imgur.com/jEj4D.png For information, there's just one user "Michael Bonfill" in each project.

Here is the output of last_query()

SELECT `p`.`project_id`, `p`.`Project`, `p`.`Description`, `p`.`Status`, `p`.`Thumbnail`, `p`.`StartDate`, `p`.`EndDate`, `t`.`template_id`, `t`.`Template`, GROUP_CONCAT(v.Name SEPARATOR ", ") as PeopleList, GROUP_CONCAT(w.asset_id SEPARATOR ", ") as AssetsList 
FROM (`projects` p) 
JOIN `assigned_projects_ppeople` a ON `a`.`project_id` = `p`.`project_id` 
JOIN `assigned_assets_pproject` w ON `w`.`project_id` = `p`.`project_id` 
JOIN `project_templates` t ON `t`.`template_id` = `p`.`template_id` 
JOIN `people` v ON `v`.`people_id` = `a`.`people_id` 
WHERE `a`.`people_id` = '1' 
GROUP BY `p`.`project_id`

I can export the SQL of my database for more info if you want. Thank you !

È stato utile?

Soluzione

use DISTINCT keyword like that GROUP_CONCAT(DISTINCT v.Name SEPARATOR ",")

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top