我正确使用GROUP_CONCAT吗?

时间:2010-09-15 19:47:42

标签: sql mysql group-concat

我正在选择属性并将它们连接到映射表,映射表将映射到位置,目标和属性类型等过滤器。

我的目标是获取所有属性,然后将它们连接到表中,然后基本上获取显示所有位置的数据,属性附加到的目标以及属性类型本身。

这是我的疑问:

SELECT p.slug                                        AS property_slug, 
       p.name                                        AS property_name, 
       p.founder                                     AS founder, 
       IF (p.display_city != '', display_city, city) AS city, 
       d.name                                        AS state,
       type
       GROUP_CONCAT( CONVERT(subcategories_id, CHAR(8)) )  AS foo,
       GROUP_CONCAT( CONVERT(categories_id, CHAR(8)) ) AS bah
     FROM properties AS p 
LEFT JOIN destinations AS d ON d.id = p.state 
LEFT JOIN regions AS r ON d.region_id = r.id 
LEFT JOIN properties_subcategories AS sc ON p.id = sc.properties_id 
LEFT JOIN categories_subcategories AS c  ON c.subcategory_id = sc.subcategories_id 
    WHERE 1 = 1 
      AND p.is_active = 1       
GROUP  BY p.id 

在我执行GROUP BYGROUP_CONCAT之前,我的数据如下所示:

id  name                  type     category_id    subcategory_id    state
--------------------------------------------------------------------------
1   The Hilton Hotel      1        1              2                 7
1   The Hilton Hotel      1        1              3                 7
1   The BlaBla Resort     2        2              5                 7

GROUP BYGROUP_CONCAT之后......

id  name                  type     category_id    subcategory_id    state
--------------------------------------------------------------------------
1   The Hilton Hotel      1        1, 1           2, 3              7
1   The BlaBla Resort     2        1              3                 7

这是一次性获取该属性的所有可能映射的首选方法,GROUP_CONCAT为这样的CSV吗?

使用这些数据,我可以渲染类似......

<div class="property" categories="1" subcategories="2,3">
   <h2>{property_name}</h2>
   <span>{property_location}</span>
</div>

然后根据用户是否点击了一个subcategory="2"属性的锚点,使用Javascript显示/隐藏它会隐藏内部没有.property的每个2subcategories属性值。

2 个答案:

答案 0 :(得分:2)

我相信你想要这样的东西:

CREATE TABLE property (id INT NOT NULL PRIMARY KEY, name TEXT);

INSERT
INTO    property
VALUES
        (1, 'Hilton'),
        (2, 'Astoria');

CREATE TABLE category (id INT NOT NULL PRIMARY KEY, property INT NOT NULL);

INSERT
INTO    category
VALUES
        (1, 1),
        (2, 1),
        (3, 2);

CREATE TABLE subcategory (id INT NOT NULL PRIMARY KEY, category INT NOT NULL);

INSERT
INTO    subcategory
VALUES
        (1, 1),
        (2, 1),
        (3, 2),
        (5, 3),
        (6, 3),
        (7, 3);


SELECT  id, name,
        CONCAT(
        '{',
        (
        SELECT  GROUP_CONCAT(
                '"', c.id, '": '
                '[',
                (
                SELECT  GROUP_CONCAT(sc.id ORDER BY sc.id SEPARATOR ', ' )
                FROM    subcategory sc
                WHERE   sc.category = c.id
                ),
                ']' ORDER BY c.id SEPARATOR ', ')
        FROM    category c
        WHERE   c.property = p.id
        ), '}')
FROM    property p;

将输出:

1   Hilton     {"1": [1, 2], "2": [3]}
2   Astoria    {"3": [5, 6, 7]}

最后一个字段是一个正确形成的JSON,它将类别ID映射到子类别id的数组。

答案 1 :(得分:1)

你应该添加DISTINCT,可能还有ORDER BY:

GROUP_CONCAT(DISTINCT CONVERT(subcategories_id, CHAR(8)) 
  ORDER BY subcategories_id)  AS foo,
GROUP_CONCAT(DISTINCT CONVERT(categories_id, CHAR(8)) 
  ORDER BY categories_id) AS bah

如果你想这样称它,它会被“去标准化”。如果这是用于渲染的最佳表示是另一个问题,我认为这很好。有些人可能会说这是黑客攻击,但我想这并不算太糟糕。

顺便说一下,“type”之后似乎缺少一个逗号。