• <tfoot id='ASUgz'></tfoot>

        <legend id='ASUgz'><style id='ASUgz'><dir id='ASUgz'><q id='ASUgz'></q></dir></style></legend>
      1. <small id='ASUgz'></small><noframes id='ASUgz'>

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

        如何在 MySQL 的 CONCAT 中使用 GROUP_CONCAT

        时间:2023-08-17
          <bdo id='NHxPG'></bdo><ul id='NHxPG'></ul>
              <tbody id='NHxPG'></tbody>

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

                • <legend id='NHxPG'><style id='NHxPG'><dir id='NHxPG'><q id='NHxPG'></q></dir></style></legend>
                  本文介绍了如何在 MySQL 的 CONCAT 中使用 GROUP_CONCAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果我在 MySQL 中有一个包含以下数据的表:

                  If I have a table with the following data in MySQL:

                  id       Name       Value
                  1          A          4
                  1          A          5
                  1          B          8
                  2          C          9
                  

                  如何将其转换为以下格式?

                  how do I get it into the following format?

                  id         Column
                  1          A:4,5,B:8
                  2          C:9
                  


                  我想我必须使用 GROUP_CONCAT.但我不确定它是如何工作的.


                  I think I have to use GROUP_CONCAT. But I'm not sure how it works.

                  推荐答案

                  select id, group_concat(`Name` separator ',') as `ColumnName`
                  from
                  (
                    select 
                      id, 
                      concat(`Name`, ':', group_concat(`Value` separator ',')) as `Name`
                    from mytbl
                    group by 
                      id, 
                      `Name`
                  ) tbl
                  group by id;
                  

                  你可以在这里看到它的实现:Sql Fiddle Demo.正是您所需要的.

                  You can see it implemented here : Sql Fiddle Demo. Exactly what you need.

                  更新分两步拆分.首先,我们得到一个表,其中包含针对唯一 [Name,id] 的所有值(逗号分隔).然后从获得的表中,我们将所有名称和值作为针对每个唯一 id 的单个值请参阅此处的说明 SQL Fiddle Demo(向下滚动有两个结果集)

                  Update Splitting in two steps. First we get a table having all values(comma separated) against a unique[Name,id]. Then from obtained table we get all names and values as a single value against each unique id See this explained here SQL Fiddle Demo (scroll down as it has two result sets)

                  编辑 阅读问题时出错,我仅按 id 分组.但是如果(值要按名称和 id 分组,然后按 id 分组),则需要两个 group_contacts.以前的答案是

                  Edit There was a mistake in reading question, I had grouped only by id. But two group_contacts are needed if (Values are to be concatenated grouped by Name and id and then over all by id). Previous answer was

                  select 
                  id,group_concat(concat(`name`,':',`value`) separator ',')
                  as Result from mytbl group by id
                  

                  你可以在这里看到它的实现:SQL Fiddle Demo

                  You can see it implemented here : SQL Fiddle Demo

                  这篇关于如何在 MySQL 的 CONCAT 中使用 GROUP_CONCAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL 和 GROUP_CONCAT() 最大长度 下一篇:如何修复:“找不到适合 jdbc:mysql://localhost/dbname

                  相关文章

                  最新文章

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

                    <tfoot id='pAP5j'></tfoot>