簡體   English   中英

MySQL查詢將表從長格式轉換為寬格式

[英]Mysql query to convert table from long format to wide format

我有一個名為ContactAttrbiutes的表,其中包含每個聯系人的屬性的列表。 為這些聯系人存儲的數據類型包括:職務,姓氏,姓氏電話號碼等。

當前表

+-------------+-----------+------------------------------+
| attributeId | ContactId | AttributeValue               |
+-------------+-----------+------------------------------+
|           1 |         5 | Lady                         |
|           2 |         5 | Elizabeth                    |
|           3 |         5 | E                            |
|           4 |         5 | Anson                        |
|           5 |         5 |                              |
|           6 |         5 |                              |
|           7 |         5 |                              |
|           8 |         5 |                              |
|          10 |         5 | 0207 72776                   |
|          11 |         5 |                              |
|          12 |         5 | 0207 22996                   |
|          13 |         5 | 0207 72761                   |
|          14 |         5 |                              |
|          15 |         5 |                              |
|          60 |         5 | Lloyds                       |
|          61 |         5 |                              |
|           1 |        10 | Mr                           |
|           2 |        10 | John                         |
|           3 |        10 | J C                          |
|           4 |        10 | Beveridge                    |
|           5 |        10 | Esq QC                       |
|           6 |        10 | Retired                      |
|           7 |        10 |                              |
|           8 |        10 |                              |
|          10 |        10 | 0207 930                     |
|          11 |        10 |                              |
|          12 |        10 |                              |
|          13 |        10 | 0207 930                     |
|          14 |        10 |                              |
|          15 |        10 |                              |
|          60 |        10 |                              |
|          61 |        10 |                              |
+-------------+-----------+------------------------------+

但是我想運行一個查詢來創建一個表,看起來像...

新表

+-----------+----------------------+-------------------------+-----------------------+------------------------+
| ContactId | AttributeValue_Title | AttributeValue_ForeName |AttributeValue_Initial | AttributeValue_Surname |
+-----------+----------------------+-------------------------+-----------------------+------------------------+
| 5         | Lady                 | Elizabeth               | E                     |  Anson                 |
+-----------+----------------------+-------------------------+-----------------------+------------------------+
| 10        | Mr                   | John                    | J C                   | Beveridge              |
+-----------+----------------------+-------------------------+-----------------------+------------------------+

我敢肯定有一個非常簡單的答案,但我已經花了數小時尋找。 有人可以幫忙嗎?

上面只是我表的一小部分,我有750,000位聯系人。 另外,我希望最終表中的列比我上面描述的要多,但它們將來自現有表的不同屬性。

提前非常感謝您。

嘗試這個

    SELECT ContactId , 
 max(CASE when attributeId = 1 then AttributeValue end) as AttributeValue_Title ,
 max(CASE when attributeId = 2 then AttributeValue end )as AttributeValue_ForeName ,
 max(CASE when attributeId = 3 then AttributeValue end )as AttributeValue_Initial ,
 max(CASE when attributeId = 4 then AttributeValue end) as AttributeValue_Surname  
 from Table1 
 group by ContactId

此處演示

  • 如果您想使其他attributeId結果更長,則只需在代碼中添加一個case語句即可。
SELECT
    t_title.AttributeValue AS title,
    t_name.AttributeValue AS name,
    ...
FROM the_table AS t_title
JOIN the_table AS t_firstname USING(contact_id)
JOIN ...
WHERE
    t_title.attributeId = 1 AND
    t_firstname.attributeId = 2 AND
    ...

在大多數情況下, EAV“模型”是一種反模式 您真的要擁有數量可變的屬性嗎? 如果是,則no-SQL解決方案可能比關系數據庫更合適。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM