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

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

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

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

        将列值设置为 SQL 查询结果中的列名

        Setting column values as column names in the SQL query result(将列值设置为 SQL 查询结果中的列名)

            • <legend id='HOxE8'><style id='HOxE8'><dir id='HOxE8'><q id='HOxE8'></q></dir></style></legend>

                <tfoot id='HOxE8'></tfoot>

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

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

                    <tbody id='HOxE8'></tbody>
                • 本文介绍了将列值设置为 SQL 查询结果中的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想读取一个表,该表的值将是 sql 查询结果的列名.例如,我将 table1 设为 ..

                  I wanted to read a table which has values which will be the column names of the sql query result. For example, I have table1 as ..

                  id    col1     col2
                  ----------------------
                  0      name    ax
                  0      name2   bx
                  0      name3   cx
                  1      name    dx
                  1      name2   ex
                  1      name3   fx                
                  

                  如果您看到 id = 0,则 name 的值为 ax,name2 为 bx,name3 为 cx.将列显示为 id、name、name2、name3 会更容易,而不是行.现在我希望查询的结果如下所示:

                  If you see for id = 0, name has value of ax and name2 is bx and name3 is cx. Instead of this being rows it would be easier to show columns as id, name, name2, name3. Now I want the result of the query to look like this:

                  id   name    name2     name3
                  0    ax      bx         cx
                  1    dx      ex         fx
                  

                  有人可以帮助我实现这一目标吗?

                  Can someone help me in achieving this?

                  推荐答案

                  这是通过数据透视表完成的.按 id 分组,您为要在列中捕获的每个值发出 CASE 语句,并使用类似 MAX() 聚合来消除空值并折叠到一行.

                  This is done with a pivot table. Grouping by id, you issue CASE statements for each value you want to capture in a column and use something like a MAX() aggregate to eliminate the nulls and collapse down to one row.

                  SELECT
                    id,
                    /* if col1 matches the name string of this CASE, return col2, otherwise return NULL */
                    /* Then, the outer MAX() aggregate will eliminate all NULLs and collapse it down to one row per id */
                    MAX(CASE WHEN (col1 = 'name') THEN col2 ELSE NULL END) AS name,
                    MAX(CASE WHEN (col1 = 'name2') THEN col2 ELSE NULL END) AS name2,
                    MAX(CASE WHEN (col1 = 'name3') THEN col2 ELSE NULL END) AS name3
                  FROM
                    yourtable
                  GROUP BY id
                  ORDER BY id
                  

                  这是一个工作示例

                  注意:这仅适用于 col1 的有限且已知数量的可能值.如果可能值的数量未知,则需要在循环中动态构建 SQL 语句.

                  Here's a working sample

                  Note: This only works as is for a finite and known number of possible values for col1. If the number of possible values is unknown, you need to build the SQL statement dynamically in a loop.

                  这篇关于将列值设置为 SQL 查询结果中的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

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

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

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