简体   繁体   English

MySQL:group_concat可以多行吗?

[英]MySQL: is it possible to group_concat multiple rows?

Here's what I want: 这就是我想要的:

attribute_name  attribute_id    attribute_value
--------------------------------------------------------------------
Appliances      16, 17, 18, 19  Washer, Dryer, Dishwasher, Microwave
Consoles        7, 3            PS3, XBox

Here's close to what I've got: 这里接近我所拥有的:

attribute_name  attribute_id   attribute_value
-------------------------------------------------
Appliances      16             Washer
Appliances      17             Dryer
Appliances      18             Dishwasher
Appliances      19             Microwave
Consoles        7              PS3
Consoles        3              XBox

...from this query: ...来自此查询:

  SELECT     a.name AS attribute_name,

             av.attribute_value_id, av.value AS attribute_value

  FROM       attribute_value av

  INNER JOIN attribute a

               ON av.attribute_id = a.attribute_id

  WHERE      av.attribute_value_id IN

               (SELECT attribute_value_id

                FROM   property_attribute

                WHERE  property_id = 1)

  ORDER BY   a.name;

I've had no success with GROUP_CONCAT. 我没有成功使用GROUP_CONCAT。 I don't even know what I want is possible. 我甚至不知道我想要什么是可能的。

Your existing query is returning everything you need to produce the concatenated columns. 您现有的查询返回生成连接列所需的所有内容。 If you wrap your existing query in a subquery, you can GROUP_CONCAT() both columns and GROUP BY attribute_name : 如果将现有查询包装在子查询中,则可以GROUP_CONCAT()列和GROUP BY attribute_name

SELECT 
  attribute_name,
  GROUP_CONCAT(attribute_value_id) AS attribute_value_ids,
  GROUP_CONCAT(attribute_value) AS attribute_values
FROM (
  /* Wrap the body of your existing query in a subselect */
  SELECT 
    a.name AS attribute_name,
    av.attribute_value_id,
    av.value AS attribute_value
  FROM  
    attribute_value av
    INNER JOIN attribute a
         ON av.attribute_id = a.attribute_id
  WHERE      
    av.attribute_value_id IN
               (SELECT attribute_value_id
                FROM   property_attribute
                WHERE  property_id = 1)
) attr_groups
GROUP BY attribute_name
ORDER BY attribute_name;
SELECT group_concat(a.name, av.attribute_value_id, av.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM