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

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

      <legend id='aVEt4'><style id='aVEt4'><dir id='aVEt4'><q id='aVEt4'></q></dir></style></legend>

        • <bdo id='aVEt4'></bdo><ul id='aVEt4'></ul>
      1. <tfoot id='aVEt4'></tfoot>
      2. MySQL - 如何将列反透视为行?

        时间:2023-08-19

          <tbody id='iFyRa'></tbody>
      3. <legend id='iFyRa'><style id='iFyRa'><dir id='iFyRa'><q id='iFyRa'></q></dir></style></legend>

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

              <bdo id='iFyRa'></bdo><ul id='iFyRa'></ul>

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

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

                  本文介绍了MySQL - 如何将列反透视为行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  此时我可能看的不是很清楚,但我在 MySQL 中有一个表,如下所示:

                  I'm probably not seeing things very clear at this moment, but I have a table in MySQL which looks like this:

                  ID | a  | b  | c 
                  1  | a1 | b1 | c1
                  2  | a2 | b2 | c2
                  

                  出于某种原因(实际上是另一个表上的连接 - 基于 ID,但我认为如果有人可以帮助我完成这部分,我可以自己完成其余部分),我需要这些行改为这样:

                  For some reason (actually a join on another table - based on ID, but I think if someone can help me out with this part, I can do the rest myself), I needed those rows to be like this instead:

                  1 | a1 | a
                  1 | b1 | b
                  1 | c1 | c
                  2 | a2 | a
                  2 | b2 | b
                  2 | c2 | c
                  

                  所以基本上,我需要查看如下行:IDcolumntitlevalue有什么方法可以轻松做到这一点吗?

                  So basically, I need to view the rows like: ID, columntitle, value Is there any way to do this easily?

                  推荐答案

                  您正在尝试反透视数据.MySQL 没有 unpivot 功能,因此您必须使用 UNION ALL 查询将列转换为行:

                  You are trying to unpivot the data. MySQL does not have an unpivot function, so you will have to use a UNION ALL query to convert the columns into rows:

                  select id, 'a' col, a value
                  from yourtable
                  union all
                  select id, 'b' col, b value
                  from yourtable
                  union all
                  select id, 'c' col, c value
                  from yourtable
                  

                  参见SQL Fiddle with Demo.

                  这也可以使用 CROSS JOIN 来完成:

                  This can also be done using a CROSS JOIN:

                  select t.id,
                    c.col,
                    case c.col
                      when 'a' then a
                      when 'b' then b
                      when 'c' then c
                    end as data
                  from yourtable t
                  cross join
                  (
                    select 'a' as col
                    union all select 'b'
                    union all select 'c'
                  ) c
                  

                  参见SQL Fiddle with Demo

                  这篇关于MySQL - 如何将列反透视为行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL删除后自动递增 下一篇:SQL 语句忽略了 where 参数

                  相关文章

                  最新文章

                  <small id='3dusI'></small><noframes id='3dusI'>

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