Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

sql - Pivot Table Using MySQL

I have two tables Triples and Tags

Triples Table has the following Columns

  id  PostID  TagID   Value
   1   1        1     Murder
   2   1        2     New Brunswick
   3   2        1     Theft
   4   2        3     Gun 

Tags Table has the following Columns

  id   TagName
   1   Incident
   2   Location
   3   Weapon    

I am trying to write sql to create a Pivot Table with Dynamic Headers

Output should be like this

       PostID  Incident   Location         Weapon        
           1   Murder     New Brunswick    
           2   Theft                        Gun 

Any help in writing the SQL would be appreciated. I have seen examples online but could not figure out this one

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to pivot the data in MySQL, you will need to use both an aggregate function and a CASE expression.

If you have a known number of columns, then you can hard-code the query:

select p.postid,
  max(case when t.tagname = 'Incident' then p.value end) Incident,
  max(case when t.tagname = 'Location' then p.value end) Location,
  max(case when t.tagname = 'Weapon' then p.value end) Weapon
from triples p
left join tags t
  on p.tagid = t.id
group by p.postid;

See SQL Fiddle with Demo

But if you have an unknown number of columns, then you will need to use a prepared statement to generate dynamic SQL:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(CASE WHEN TagName = ''',
      TagName,
      ''' THEN p.value END) AS `',
      TagName, '`'
    )
  ) INTO @sql
FROM tags;


SET @sql 
  = CONCAT('SELECT p.postid, ', @sql, ' 
           from triples p
           left join tags t
             on p.tagid = t.id
           group by p.postid');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

See SQL Fiddle with Demo.

Both will give the result:

| POSTID | INCIDENT |      LOCATION | WEAPON |
----------------------------------------------
|      1 |   Murder | New Brunswick | (null) |
|      2 |    Theft |        (null) |    Gun |

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...