<small id='4COCF'></small><noframes id='4COCF'>

    • <bdo id='4COCF'></bdo><ul id='4COCF'></ul>
  1. <tfoot id='4COCF'></tfoot>
    <i id='4COCF'><tr id='4COCF'><dt id='4COCF'><q id='4COCF'><span id='4COCF'><b id='4COCF'><form id='4COCF'><ins id='4COCF'></ins><ul id='4COCF'></ul><sub id='4COCF'></sub></form><legend id='4COCF'></legend><bdo id='4COCF'><pre id='4COCF'><center id='4COCF'></center></pre></bdo></b><th id='4COCF'></th></span></q></dt></tr></i><div id='4COCF'><tfoot id='4COCF'></tfoot><dl id='4COCF'><fieldset id='4COCF'></fieldset></dl></div>

    1. <legend id='4COCF'><style id='4COCF'><dir id='4COCF'><q id='4COCF'></q></dir></style></legend>

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

      时间:2023-08-19

      • <bdo id='6emn2'></bdo><ul id='6emn2'></ul>
            <tfoot id='6emn2'></tfoot>
              <tbody id='6emn2'></tbody>

            1. <legend id='6emn2'><style id='6emn2'><dir id='6emn2'><q id='6emn2'></q></dir></style></legend>
                <i id='6emn2'><tr id='6emn2'><dt id='6emn2'><q id='6emn2'><span id='6emn2'><b id='6emn2'><form id='6emn2'><ins id='6emn2'></ins><ul id='6emn2'></ul><sub id='6emn2'></sub></form><legend id='6emn2'></legend><bdo id='6emn2'><pre id='6emn2'><center id='6emn2'></center></pre></bdo></b><th id='6emn2'></th></span></q></dt></tr></i><div id='6emn2'><tfoot id='6emn2'></tfoot><dl id='6emn2'><fieldset id='6emn2'></fieldset></dl></div>
              1. <small id='6emn2'></small><noframes id='6emn2'>

                本文介绍了我可以将多个 MySQL 行连接到一个字段中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                使用 MySQL,我可以执行以下操作:

                Using MySQL, I can do something like:

                SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
                

                我的输出:

                shopping
                fishing
                coding
                

                但我只想要 1 行,1 列:

                but instead I just want 1 row, 1 col:

                预期输出:

                shopping, fishing, coding
                

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

                The reason is that I'm selecting multiple values from multiple tables, and after all the joins I've got a lot more rows than I'd like.

                我在 上寻找了一个函数MySQL Doc 它看起来不像 CONCATCONCAT_WS 函数接受结果集.

                I've looked for a function on MySQL Doc and it doesn't look like the CONCAT or CONCAT_WS functions accept result sets.

                这里有人知道怎么做吗?

                So does anyone here know how to do this?

                推荐答案

                您可以使用 GROUP_CONCAT:

                You can use GROUP_CONCAT:

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

                正如 Ludwig 在他的评论中所述, 您可以添加 DISTINCT 运算符以避免重复:

                As Ludwig stated in his comment, you can add the DISTINCT operator to avoid duplicates:

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

                正如 Jan 在他们的评论中所述, 您还可以在使用 ORDER BY 内爆之前对值进行排序:

                As Jan stated in their comment, you can also sort the values before imploding it using ORDER BY:

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

                正如 Dag 在他的评论中所述, 结果有 1024 字节的限制.要解决此问题,请在查询之前运行此查询:

                As Dag stated in his comment, there is a 1024 byte limit on the result. To solve this, run this query before your query:

                SET group_concat_max_len = 2048;
                

                当然,您可以根据需要更改2048.计算和赋值:

                Of course, you can change 2048 according to your needs. To calculate and assign the value:

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

                这篇关于我可以将多个 MySQL 行连接到一个字段中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:INNER JOIN ON vs WHERE 子句 下一篇:如何在 MySQL 中进行正则表达式替换?

                相关文章

                最新文章

                <tfoot id='IDWMQ'></tfoot>
                <i id='IDWMQ'><tr id='IDWMQ'><dt id='IDWMQ'><q id='IDWMQ'><span id='IDWMQ'><b id='IDWMQ'><form id='IDWMQ'><ins id='IDWMQ'></ins><ul id='IDWMQ'></ul><sub id='IDWMQ'></sub></form><legend id='IDWMQ'></legend><bdo id='IDWMQ'><pre id='IDWMQ'><center id='IDWMQ'></center></pre></bdo></b><th id='IDWMQ'></th></span></q></dt></tr></i><div id='IDWMQ'><tfoot id='IDWMQ'></tfoot><dl id='IDWMQ'><fieldset id='IDWMQ'></fieldset></dl></div>

                1. <legend id='IDWMQ'><style id='IDWMQ'><dir id='IDWMQ'><q id='IDWMQ'></q></dir></style></legend>

                  • <bdo id='IDWMQ'></bdo><ul id='IDWMQ'></ul>

                    <small id='IDWMQ'></small><noframes id='IDWMQ'>