代码之家  ›  专栏  ›  技术社区  ›  Glyn

MySQL-如何将行连接成一个[重复]

  •  0
  • Glyn  · 技术社区  · 3 年前

    使用 MySQL ,我可以这样做:

    SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
    

    我的输出:

    shopping
    fishing
    coding
    

    但我只想要一行一列:

    预期产出:

    shopping, fishing, coding
    

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

    我在上面找了一个函数 MySQL Doc 而且看起来不像 CONCAT CONCAT_WS 函数接受结果集。

    这里有人知道怎么做吗?

    0 回复  |  直到 5 年前
        1
  •  0
  •   Muhammad Shahzad    4 年前

    你可以用 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);
    
        2
  •  0
  •   Golden Lion    4 年前

    看看 GROUP_CONCAT the documentation 更多细节。

    它看起来像:

      SELECT GROUP_CONCAT(hobbies SEPARATOR ', ') 
      FROM peoples_hobbies 
      WHERE person_id = 5 
      GROUP BY 'all';
    
        3
  •  0
  •   Md. Tarikul Islam Soikot    4 年前

    连接的替代语法 多个单独的行

    警告:这篇帖子会让你感到饥饿。

    鉴于:

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

    假设您有一个产品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_CONCAT the 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 |
    +------------------------------------------------+-------+
    
        4
  •  -1
  •   Payel Senapati    4 年前

    您可以更改 GROUP_CONCAT 通过设置 group_concat_max_len 参数

    详见 MySQL documantation .