動態列 MySQL 資料透視表查詢
P粉262113569
P粉262113569 2023-08-27 17:24:10
0
2
441
<p>我使用下表來儲存產品資料:</p> <pre class="brush:php;toolbar:false;">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 | ----------------- ------------ </pre> <p>使用以下查詢從兩個表中選擇記錄</p> <pre class="brush:php;toolbar:false;">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 | --------------- --------------- -------- --------- --- ----- </pre> <p>一切正常:) </p> <p>因為我動態填入「附加」表,如果查詢也是動態的,那就太好了。這樣我就不必每次輸入新的欄位名稱和欄位值時都更改查詢。 </p>
P粉262113569
P粉262113569

全部回覆(2)
P粉604848588

我的做法與接受的答案略有不同。這樣您就可以避免使用 GROUP_CONCAT,它預設為 1024 個字符,如果您有很多字段,則該限制將不起作用,除非您更改限制。

SET @sql = '';
SELECT
    @sql := CONCAT(@sql,if(@sql='','',', '),temp.output)
FROM
(
    SELECT
      DISTINCT
        CONCAT(
         'MAX(IF(pa.fieldname = ''',
          fieldname,
          ''', pa.fieldvalue, NULL)) AS ',
          fieldname
        ) as output
    FROM
        product_additional
) as temp;

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;

點擊此處查看示範

#
P粉495955986

MySQL 中動態執行此操作的唯一方法是使用已準備好的語句。這是一篇關於它們的好文章:

動態資料透視表(將行轉換為列)

#您的程式碼將如下所示:

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板