为什么这个group_concat不起作用?

时间:2013-04-07 06:15:14

标签: mysql sql database hierarchical-data

我不明白为什么这个GROUP_CONCAT不起作用,就外部查询而言,返回了3行所以我想通过它来组合group_concat但是它不喜欢它...

http://sqlfiddle.com/#!2/24764/3

CREATE TABLE nested_category (
        category_id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(20) NOT NULL,
        lft INT NOT NULL,
        rgt INT NOT NULL
);

INSERT INTO nested_category VALUES(1,'ELECTRONICS',1,20),(2,'TELEVISIONS',2,9),(3,'TUBE',3,4),
 (4,'LCD',5,6),(5,'PLASMA',7,8),(6,'PORTABLE ELECTRONICS',10,19),(7,'MP3 PLAYERS',11,14),(8,'FLASH',12,13),
 (9,'CD PLAYERS',15,16),(10,'2 WAY RADIOS',17,18);

SELECT GROUP_CONCAT(rs.category_id, ',')
FROM 
    (
        SELECT node.category_id, node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
        FROM nested_category AS node,
                nested_category AS parent,
                nested_category AS sub_parent,
                (
                        SELECT node.category_id, node.name, (COUNT(parent.name) - 1) AS depth
                        FROM nested_category AS node,
                                nested_category AS parent
                        WHERE node.lft BETWEEN parent.lft AND parent.rgt
                            AND node.name = 'PORTABLE ELECTRONICS'
                        GROUP BY node.name
                        ORDER BY node.lft
                )AS sub_tree
        WHERE node.lft BETWEEN parent.lft AND parent.rgt
                AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
                AND sub_parent.name = sub_tree.name
        GROUP BY node.name
        HAVING depth = 1
        ORDER BY node.lft
    ) as rs
GROUP BY rs.category_id

2 个答案:

答案 0 :(得分:9)

两件事:

首先:将GROUP_CONCAT(rs.category_id, ',')更改为GROUP_CONCAT(rs.category_id)

逗号是默认分隔符,from the docs您可以看到,如果您想要更改分隔符,就像 GROUP_CONCAT(rs.category_id SEPARATOR '|')

第二:删除最后一个: GROUP BY rs.category_id

如果您group by每个category_id,则表示每个category_id都在其自己的集合中,因此group_concat每行只有一个类别。

http://sqlfiddle.com/#!2/24764/7

答案 1 :(得分:1)

尝试此查询

SELECT GROUP_CONCAT(rs.category_id)
FROM 
    (
        SELECT node.category_id, node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
        FROM nested_category AS node,
                nested_category AS parent,
                nested_category AS sub_parent,
                (
                        SELECT node.category_id, node.name, (COUNT(parent.name) - 1) AS depth
                        FROM nested_category AS node,
                                nested_category AS parent
                        WHERE node.lft BETWEEN parent.lft AND parent.rgt
                            AND node.name = 'PORTABLE ELECTRONICS'
                        GROUP BY node.name
                        ORDER BY node.lft
                )AS sub_tree
        WHERE node.lft BETWEEN parent.lft AND parent.rgt
                AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
                AND sub_parent.name = sub_tree.name
        GROUP BY node.name
        HAVING depth = 1
        ORDER BY node.lft
    ) as rs

<强>输出

| GROUP_CONCAT(RS.CATEGORY_ID) |
--------------------------------
|                       7,9,10 |