首页 文章

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

提问于
浏览
1015

使用 MySQL ,我可以这样做:

SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;

My Output:

shopping
fishing
coding

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

Expected Output:

shopping, fishing, coding

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

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

10 回答

  • 1457

    我有一个更复杂的查询,发现我必须在外部查询中使用 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;
    

    希望这可以帮助某人 .

  • 13

    试试这个:

    DECLARE @Hobbies NVARCHAR(200) = ' '
    
    SELECT @Hobbies = @Hobbies + hobbies + ',' FROM peoples_hobbies WHERE person_id = 5;
    
  • 59

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

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

    用于连接多个单独行的备用语法

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

    鉴于:

    我发现自己想要 select 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,请道歉......

  • 0

    有一个GROUP Aggregate函数,GROUP_CONCAT .

  • 6

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

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

    那么你可以得到

    test1,test11
    
  • 19

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

    请参阅MySQL documantation中的详细信息 .

  • 24

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

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

    要么

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

    你可以使用GROUP_CONCAT

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

    正如Ludwig在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
    );
    
  • 10

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

    它看起来像:

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

相关问题