Domanda

I have multiple tables that I need to concatenate a group of results from. There is 6 tables overall with a index in each matching the primary_key in the initial table.

However, when I execute my query it results in multiple instances of each result being returned for each table. If i select just one record the output is fine. It seems that the query is being ran multiple times per table and all the results are being concatenated together.

    SELECT id, 
    GROUP_CONCAT(tb1.table1) AS tbl1,
    GROUP_CONCAT(tb2.table2) AS tbl2,
    GROUP_CONCAT(tb3.table3) AS tbl3,
    GROUP_CONCAT(tb4.table4) AS tbl4,
    GROUP_CONCAT(tb5.table5 )AS tbl5
    FROM table t
    LEFT OUTER JOIN (
        SELECT id, field2
        FROM table1
        GROUP BY id
    ) tb1
    ON tb1.id = t.id
    LEFT OUTER JOIN (
        SELECT id, field2
        FROM table2
        GROUP BY id
    ) tb2
    ON tb2.id = t.id
    LEFT OUTER JOIN (
        SELECT id, field2
        FROM table3
        GROUP BY id
    ) tb3
    ON tb3.id = t.id
    LEFT OUTER JOIN (
        SELECT id, field2
        FROM table4
        GROUP BY id
    ) tb4
    ON tb4.id = t.id
    LEFT OUTER JOIN (
        SELECT id, field2
        FROM table5
        GROUP BY id
   ) tb5
   ON tb5.id = t.id
   GROUP BY t.id

I expected/want the results to be.

Primary Key | Field 1 | Field 2 | Field 3 | Field 4 | Field 5
1           |  1, 2   |  2, 3   |  2, 4   |  1, 5   |  NUll

But the returned results are, this is only an example of 1 row when not specifying a record id, however all rows appear like this.

Primary Key | Field 1 | Field 2 | Field 3 | Field 4 | Field 5
1           | 1,2     | 2,3,    | 2,4     | 1,5,    |  NUll
2           | 1,2,1,  | 2,3,2,  | 2,4,2,  | 1,5,1,  |  1
2           | 1,2,1,2 | 2,3,2,3 | 2,4,2,4 | 1,5,1,5 |  1, 2

I am at a loss where or why the query is iterating over itself multiple times and then iterating over each table.

Any Help would be greatly appreciated.

Regards,

Edit: I have updated my initial desired results and added more rows to how the table is being brought back.

Also included a 4 table schema: CREATE TABLE IF NOT EXISTS table ( id int(11) NOT NULL AUTO_INCREMENT, Name varchar(255) DEFAULT NULL, PRIMARY KEY (id) );

CREATE TABLE IF NOT EXISTS `table2` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `table1id` int(11) NOT NULL,
    `Name` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX (`table1id`)
);

CREATE TABLE IF NOT EXISTS `table3` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `table1id` int(11) NOT NULL,
    `Name` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX (`table1id`)
);

CREATE TABLE IF NOT EXISTS `table4` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `table1id` int(11) NOT NULL,
    `Name` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX (`table1id`)
);

INSERT INTO `table`(`id`, `Name`) VALUES (1, 'Test'),(2, 'Second Test'),(3,'Third Test'),(4, 'Test Four'),(5,'Test Five');
INSERT INTO `table2` (`id`,`table1id`, `Name`) VALUES (1,1,'Test Value'), (2,2, 'Test Value 2'), (3,3, 'Test Value 3');
INSERT INTO `table3` (`id`,`table1id`, `Name`) VALUES (1,2,'Value'), (2,3, 'Value 2'), (3,4, 'Value 3');
INSERT INTO `table4` (`id`,`table1id`, `Name`) VALUES (1,1,'Test'), (2,2, 'Test 2'), (3,5, 'Test 3');
È stato utile?

Soluzione

Use DISTINCT for all columns in GROUP_CONCAT()

Try this:

SELECT t.id, 
       GROUP_CONCAT(DISTINCT tb1.field2) AS tbl1,
       GROUP_CONCAT(DISTINCT tb2.field2) AS tbl2,
       GROUP_CONCAT(DISTINCT tb3.field2) AS tbl3,
       GROUP_CONCAT(DISTINCT tb4.field2) AS tbl4,
       GROUP_CONCAT(DISTINCT tb5.field2) AS tbl5
FROM TABLE t
LEFT OUTER JOIN table1 tb1 ON tb1.id = t.id
LEFT OUTER JOIN table2 tb2 ON tb2.id = t.id
LEFT OUTER JOIN table3 tb3 ON tb3.id = t.id
LEFT OUTER JOIN table4 tb4 ON tb4.id = t.id
LEFT OUTER JOIN table5 tb5 ON tb5.id = t.id
GROUP BY t.id
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top