MySQL group_concat问题

时间:2014-01-07 12:46:57

标签: mysql sql select group-by group-concat

我需要多个表来连接一组结果。总共有6个表,每个表都与初始表中的primary_key匹配。

但是,当我执行查询时,会导致为每个表返回每个结果的多个实例。如果我只选择一条记录,则输出正常。似乎每个表都运行了多次查询,并且所有结果都被连接在一起。

    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

我希望/希望得到结果。

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

但返回的结果是,这只是未指定记录ID时的1行示例,但是所有行都显示为这样。

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

我不知道查询在多次重复迭代,然后在每个表上进行迭代。

非常感谢任何帮助。

此致

编辑:我已更新了我最初的预期结果,并添加了更多行以了解该表的恢复方式。

还包括一个4表架构:     CREATE TABLE IF NOT NOT EXISTS table(         id int(11)NOT NULL AUTO_INCREMENT,         Name varchar(255)DEFAULT NULL,         主要关键(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');

1 个答案:

答案 0 :(得分:2)

DISTINCT

中的所有列使用GROUP_CONCAT()

试试这个:

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