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

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

      1. <legend id='zaBrr'><style id='zaBrr'><dir id='zaBrr'><q id='zaBrr'></q></dir></style></legend>
        • <bdo id='zaBrr'></bdo><ul id='zaBrr'></ul>

        带有动态列的 MySQL 数据透视表查询

        时间:2023-08-20

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

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

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

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

                  问题描述

                  我使用下表来存储产品数据:

                  I'm using the following tables for storing product data:

                  mysql> SELECT * FROM product;
                  +---------------+---------------+--------+
                  | id | name     | description   | stock  |
                  +---------------+---------------+--------+
                  |  1 | product1 | first product |    5   | 
                  |  2 | product2 | second product|    5   | 
                  +---------------+---------------+--------+
                  
                  mysql> SELECT * FROM product_additional;
                  +-----------------+------------+
                  | id | fieldname  | fieldvalue |
                  +-----------------+------------+
                  |  1 | size       | S          |
                  |  1 | height     | 103        |
                  |  2 | size       | L          |
                  |  2 | height     | 13         |
                  |  2 | color      | black      |
                  +-----------------+------------+
                  

                  使用以下查询从两个表中选择记录

                  Using the following query to select the records from both tables

                  mysql> SELECT 
                      p.id
                      , p.name
                      , p.description
                      ,MAX(IF(pa.fieldname = 'size', pa.fieldvalue, NULL)) as `size`
                      ,MAX(IF(pa.fieldname = 'height', pa.fieldvalue, NULL)) as `height`
                      ,MAX(IF(pa.fieldname = 'color', pa.fieldvalue, NULL)) as `color`
                  FROM product p
                  LEFT JOIN product_additional AS pa ON p.id = pa.id
                  GROUP BY p.id
                  +---------------+---------------+--------+---------+--------+
                  | id | name     | description   | size   | height  | color  |
                  +---------------+---------------+--------+---------+--------+
                  |  1 | product1 | first product | S      | 103     | null   |
                  |  2 | product2 | second product| L      | 13      | black  |
                  +---------------+---------------+--------+---------+--------+
                  

                  而且一切正常:)

                  因为我动态填充附加"表,如果查询也是动态的,那就太好了.这样我就不必每次输入新的字段名和字段值时都更改查询.

                  Because i fill the 'additional' table dynamically it would be nice, if the query would also be dynamic. In that way i dont have to change the query everytime i put in a new fieldname and fieldvalue.

                  推荐答案

                  在 MySQL 中动态执行此操作的唯一方法是使用 Prepared 语句.这是一篇关于它们的好文章:

                  The only way in MySQL to do this dynamically is with Prepared statements. Here is a good article about them:

                  动态数据透视表(将行转换为列)

                  您的代码如下所示:

                  SET @sql = NULL;
                  SELECT
                    GROUP_CONCAT(DISTINCT
                      CONCAT(
                        'MAX(IF(pa.fieldname = ''',
                        fieldname,
                        ''', pa.fieldvalue, NULL)) AS ',
                        fieldname
                      )
                    ) INTO @sql
                  FROM product_additional;
                  
                  SET @sql = CONCAT('SELECT p.id
                                      , p.name
                                      , p.description, ', @sql, ' 
                                     FROM product p
                                     LEFT JOIN product_additional AS pa 
                                      ON p.id = pa.id
                                     GROUP BY p.id, p.name, p.description');
                  
                  PREPARE stmt FROM @sql;
                  EXECUTE stmt;
                  DEALLOCATE PREPARE stmt;
                  

                  参见演示

                  注意:GROUP_CONCAT 函数有 1024 个字符的限制.参见参数 group_concat_max_len

                  NOTE: GROUP_CONCAT function has a limit of 1024 characters. See parameter group_concat_max_len

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

                  上一篇:左外连接不会从我的左表中返回所有行? 下一篇:如何在 Python 中连接到 MySQL 数据库?

                  相关文章

                  最新文章

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

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