我可以将多个MySQL行连接到一个字段中吗?

问题描述 投票:1079回答:11

使用MySQL,我可以做类似的事情:

SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;

我的输出:

shopping
fishing
coding

但相反,我只想要1行,1 col:

预期产出:

shopping, fishing, coding

原因是我从多个表中选择了多个值,并且在所有连接之后,我有比我想要的更多的行。

我在MySQL Doc上寻找了一个函数,它看起来不像CONCATCONCAT_WS函数接受结果集,所以这里的任何人都知道如何做到这一点吗?

mysql sql concat group-concat
11个回答
1575
投票

你可以使用GROUP_CONCAT

SELECT person_id, GROUP_CONCAT(hobbies SEPARATOR ', ')
FROM peoples_hobbies
GROUP BY person_id;

正如路德维希在his comment,中所述,您可以添加DISTINCT运算符以避免重复:

SELECT person_id, GROUP_CONCAT(DISTINCT hobbies SEPARATOR ', ')
FROM peoples_hobbies 
GROUP BY person_id;

正如Jan在their comment,中所述,您还可以使用ORDER BY对值进行排序:

SELECT person_id, GROUP_CONCAT(hobbies ORDER BY hobbies ASC SEPARATOR ', ')
FROM peoples_hobbies
GROUP BY person_id;

正如Dag在his comment,中所说,结果有1024字节的限制。要解决此问题,请在查询之前运行此查询:

SET group_concat_max_len = 2048;

当然,您可以根据需要更改2048。要计算和分配值:

SET group_concat_max_len = CAST(
    (SELECT SUM(LENGTH(hobbies)) + COUNT(*) * LENGTH(', ')
    FROM peoples_hobbies 
    GROUP BY person_id)
    AS UNSIGNED
);

1
投票

对于有人在这里看如何使用GROUP_CONCAT与子查询 - 发布此示例

SELECT i.*,
(SELECT GROUP_CONCAT(userid) FROM favourites f WHERE f.itemid = i.id) AS idlist
FROM items i
WHERE i.id = $someid

所以GROUP_CONCAT必须在子查询中使用,而不是包装它。


0
投票

我们有两种方法来连接MySql中的列

select concat(hobbies) as `Hobbies` from people_hobbies where 1

要么

select group_concat(hobbies) as `Hobbies` from people_hobbies where 1

94
投票

如果您的MySQL版本(4.1)支持它,请查看GROUP_CONCAT。有关详细信息,请参阅the documentation

它看起来像:

  SELECT GROUP_CONCAT(hobbies SEPARATOR ', ') 
  FROM peoples_hobbies 
  WHERE person_id = 5 
  GROUP BY 'all';

63
投票

Alternate syntax to concatenate multiple, individual rows

警告:这篇文章会让你感到饥饿。

鉴于:

我发现自己想要选择多个单独的行而不是组 - 并在某个字段上连接。

假设您有一张产品ID及其名称和价格表:

+------------+--------------------+-------+
| product_id | name               | price |
+------------+--------------------+-------+
|         13 | Double Double      |     5 |
|         14 | Neapolitan Shake   |     2 |
|         15 | Animal Style Fries |     3 |
|         16 | Root Beer          |     2 |
|         17 | Lame T-Shirt       |    15 |
+------------+--------------------+-------+

然后你有一些花哨的schmancy ajax将这些小狗列为复选框。

你饥饿的河马用户选择13, 15, 16。今天她没有甜点......

找:

使用纯mysql在一行中汇总用户订单的方法。

解:

使用GROUP_CONCATthe IN clause

mysql> SELECT GROUP_CONCAT(name SEPARATOR ' + ') AS order_summary FROM product WHERE product_id IN (13, 15, 16);

哪个输出:

+------------------------------------------------+
| order_summary                                  |
+------------------------------------------------+
| Double Double + Animal Style Fries + Root Beer |
+------------------------------------------------+

奖金解决方案:

如果你想要总价格,请投资SUM()

mysql> SELECT GROUP_CONCAT(name SEPARATOR ' + ') AS order_summary, SUM(price) AS total FROM product WHERE product_id IN (13, 15, 16);
+------------------------------------------------+-------+
| order_summary                                  | total |
+------------------------------------------------+-------+
| Double Double + Animal Style Fries + Root Beer |    10 |
+------------------------------------------------+-------+

PS:如果你附近没有In-N-Out,请道歉...


35
投票

您可以通过设置GROUP_CONCAT参数来更改group_concat_max_len值的最大长度。

查看MySQL documantation中的详细信息。


25
投票

有一个GROUP Aggregate函数,GROUP_CONCAT


22
投票

在我的情况下,我有一行ID,有必要将其转换为char,否则,结果被编码为二进制格式:

SELECT CAST(GROUP_CONCAT(field SEPARATOR ',') AS CHAR) FROM table

14
投票

使用MySQL(5.6.13)会话变量和赋值运算符,如下所示

SELECT @logmsg := CONCAT_ws(',',@logmsg,items) FROM temp_SplitFields a;

那么你可以得到

test1,test11

12
投票

我有一个更复杂的查询,发现我必须在外部查询中使用GROUP_CONCAT才能使其工作:

原始查询:

SELECT DISTINCT userID 
FROM event GROUP BY userID 
HAVING count(distinct(cohort))=2);

崩盘:

SELECT GROUP_CONCAT(sub.userID SEPARATOR ', ') 
FROM (SELECT DISTINCT userID FROM event 
GROUP BY userID HAVING count(distinct(cohort))=2) as sub;

希望这可以帮助某人。


7
投票

试试这个:

DECLARE @Hobbies NVARCHAR(200) = ' '

SELECT @Hobbies = @Hobbies + hobbies + ',' FROM peoples_hobbies WHERE person_id = 5;
© www.soinside.com 2019 - 2024. All rights reserved.