• <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 查询结果中的列名

        时间:2023-05-23

            • <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 查询结果中的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 MySQL 查询中使用 SELECT 和 LIMIT 时如何计算所有行? 下一篇:如何在连接字段中使用逗号分隔列表连接两个表

                  相关文章

                      <bdo id='DmxIx'></bdo><ul id='DmxIx'></ul>
                  1. <tfoot id='DmxIx'></tfoot>
                  2. <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'>