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

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

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

        MySQL动态数据透视表

        MySQL dynamic pivot table(MySQL动态数据透视表)
          <bdo id='e6f6y'></bdo><ul id='e6f6y'></ul>

                <tfoot id='e6f6y'></tfoot>
                <legend id='e6f6y'><style id='e6f6y'><dir id='e6f6y'><q id='e6f6y'></q></dir></style></legend>
              • <small id='e6f6y'></small><noframes id='e6f6y'>

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

                1. 本文介绍了MySQL动态数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试使用具有动态列的数据透视表来工作.当 user_id 是一个字符串时,它工作正常,但如果它是一个 int,那么它似乎失败

                  以下是我在过去问题的帮助下到目前为止所得到的:

                  CREATE TABLE measure2(`inspection_date` 日期,`user_id` int,`score` int,评论文本);INSERT INTO measure2(`inspection_date`、`user_id`、`score`、评论)价值观('2012-10-16', 0, 0, null),('2012-10-16', 1, 0, null),('2012-10-16', 2, 0, null),('2012-10-16', 3, 0, null),('2012-10-17', 0, 1, null),('2012-10-17', 1, 1, null),('2012-10-17', 2, 1, null),('2012-10-18', 3, 1, null);SET @sql = NULL;选择GROUP_CONCAT(DISTINCT)康卡特('max(user_id = '''的情况,用户身份,''' 然后计分结束) AS ',用户身份)) 进入@sql从措施2;SET @sql = CONCAT('选择检查日期,',@sql,'从措施 2GROUP BY 检查日期');从@sql 准备 stmt;执行 stmt;解除分配准备 stmt;

                  参见:http://sqlfiddle.com/#!2/eab24/1

                  我确定它很简单,但我错过了什么?

                  谢谢

                  解决方案

                  由于值在 int 中,因此您将它们设为列名,因此必须将值括在反引号中

                  sql 将如下所示:

                  max(case when user_id = 1 then score end) as `1`

                  完整的查询将是:

                  SET @sql = NULL;选择GROUP_CONCAT(DISTINCT)康卡特('max(user_id = '''的情况,用户身份,''' 然后计分结束) AS `',用户 ID, '`')) 进入@sql从措施2;SET @sql = CONCAT('选择检查日期,',@sql,'从措施 2GROUP BY 检查日期');从@sql 准备 stmt;执行 stmt;解除分配准备 stmt;

                  参见SQL Fiddle with Demo

                  Im trying to get a pivot table with dynamic columns to work. When user_id is a string, it works fine but if its an int, then it seems to fail

                  Here is what I have so far with the assistance of past questions:

                  CREATE TABLE measure2
                      (`inspection_date` date, `user_id` int, `score` int, comment text)
                  ;
                  
                  INSERT INTO measure2
                      (`inspection_date`, `user_id`, `score`, comment)
                  VALUES
                      ('2012-10-16', 0, 0, null),
                      ('2012-10-16', 1, 0, null),
                      ('2012-10-16', 2, 0, null),
                      ('2012-10-16', 3, 0, null),
                      ('2012-10-17', 0, 1, null),
                      ('2012-10-17', 1, 1, null),
                      ('2012-10-17', 2, 1, null),
                      ('2012-10-18', 3, 1, null)
                  ;
                  
                  
                  SET @sql = NULL;
                  SELECT
                    GROUP_CONCAT(DISTINCT
                      CONCAT(
                        'max(case when user_id = ''',
                        user_id,
                        ''' then score end) AS ',
                        user_id
                      )
                    ) INTO @sql
                  FROM  measure2;
                  
                  SET @sql = CONCAT('SELECT inspection_date, ', @sql, '
                                    FROM measure2
                                    GROUP BY inspection_date');
                  
                  PREPARE stmt FROM @sql;
                  EXECUTE stmt;
                  DEALLOCATE PREPARE stmt;
                  

                  see: http://sqlfiddle.com/#!2/eab24/1

                  Im sure its something simple, but what am I missing?

                  Thanks

                  解决方案

                  Since the values are in int you are are making them the column names, you have to wrap the values in a backtick

                  The sql will look like:

                  max(case when user_id = 1 then score end) as `1`
                  

                  The full query will be:

                  SET @sql = NULL;
                  SELECT
                    GROUP_CONCAT(DISTINCT
                      CONCAT(
                        'max(case when user_id = ''',
                        user_id,
                        ''' then score end) AS `',
                        user_id, '`'
                      )
                    ) INTO @sql
                  FROM  measure2;
                  
                  SET @sql = CONCAT('SELECT inspection_date, ', @sql, ' 
                                    FROM measure2 
                                    GROUP BY inspection_date');
                  
                  PREPARE stmt FROM @sql;
                  EXECUTE stmt;
                  DEALLOCATE PREPARE stmt;
                  

                  See SQL Fiddle with Demo

                  这篇关于MySQL动态数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                  MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                  Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                  How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                  Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                  Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)
                        <tbody id='0VKDZ'></tbody>

                      <small id='0VKDZ'></small><noframes id='0VKDZ'>

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

                        <bdo id='0VKDZ'></bdo><ul id='0VKDZ'></ul>

                        • <tfoot id='0VKDZ'></tfoot>