SQL – 如何转置?

我有类似下表的内容:

================================================
| Id | UserId | FieldName     | FieldValue     |
=====+========+===============+================|
| 1  | 100    | Username      | John Doe       |
|----+--------+---------------+----------------|
| 2  | 100    | Password      | pass123!       |
|----+--------+---------------+----------------|
| 3  | 102    | Username      | Jane           |
|----+--------+---------------+----------------|
| 4  | 102    | Password      | $ecret         |
|----+--------+---------------+----------------|
| 5  | 102    | Email Address | jane@email.com |
------------------------------------------------

我需要一个查询,它会给我一个这样的结果:

==================================================
| UserId | Username  | Password | Email Address  |
=========+===========+===========================|
| 100    | John Doe  | pass123! |                |
|--------+-----------+----------+----------------|
| 102    | Jane      | $ecret   | jane@email.com |
|--------+-----------+----------+----------------|

请注意,FieldName中的值不限于用户名,密码和电子邮件地址.它们可以是用户定义的任何东西.

有没有办法在SQL中执行此操作?

解决方法:

MySQL不支持ANSI PIVOT / UNPIVOT语法,因此让您使用:

  SELECT t.userid
         MAX(CASE WHEN t.fieldname = 'Username' THEN t.fieldvalue ELSE NULL END) AS Username,
         MAX(CASE WHEN t.fieldname = 'Password' THEN t.fieldvalue ELSE NULL END) AS Password,
         MAX(CASE WHEN t.fieldname = 'Email Address' THEN t.fieldvalue ELSE NULL END) AS Email
    FROM TABLE t
GROUP BY t.userid

如您所见,CASE语句需要按值定义.要使这种动态,您需要使用MySQL’s Prepared Statement (dynamic SQL) syntax.

上一篇:mysql – 在SQL查询结果中将列值设置为列名


下一篇:MySQL中的数据透视表 – 使用varchar格式的值转换为数据透视表