Mysql, reshape data from long / tall to wide

前端 未结 3 772
Happy的楠姐
Happy的楠姐 2020-12-02 23:26

I have data in a mysql table in long / tall format (described below) and want to convert it to wide format. Can I do this using just sql?

Easiest to explain with an

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 23:55

    I think I found the solution, which uses VIEWS and INSERT INTO (as suggested by e4c5).

    You have to get your list of AttrNames/Keys yourself, but MYSQL does the other heavy lifting.

    For the simple test case above, create the new_table with the appropriate columns (don't forget to have an auto-increment primary key as well). Then

    CREATE VIEW a
    AS SELECT country, attrValue
    WHERE attrName="President";
    
    CREATE VIEW b
    AS SELECT country, attrValue
    WHERE attrName="Currency";
    
    
    INSERT INTO newtable(country, President, Currency)
    SELECT a.country, a.attrValue, b.attrValue
    FROM  a
    INNER JOIN b  ON a.country=b.country;
    

    If you have more attrNames, then create one view for each one and then adjust the last statement accordingly.

    INSERT INTO newtable(country, President, Currency, Capital, Population)
    SELECT a.country, a.attrValue, b.attrValue, c.attrValue, d.attrValue
    FROM  a
    INNER JOIN b  ON a.country=b.country
    INNER JOIN c  ON a.country=c.country
    INNER JOIN d  ON a.country=d.country;
    

    Some more tips

    • use NATURAL LEFT JOIN and you don't have to specify the ON clause

提交回复
热议问题