从mySQL中的表重新显示数据

时间:2012-02-18 17:37:17

标签: mysql

我正在尝试重新显示表格中的数据

目前表中的数据为

 __________________________________________
|                 UserMeta                 |
|__________________________________________|
| id | userID | orgID |  keyType | keyData |
|____|________|_______|__________|_________|
|  1 |      1 |     1 | UserName | Scarlet |
|  2 |      1 |     1 |      Age |      23 |
|  3 |      2 |     2 | UserName |   Diana |
|  4 |      2 |     2 |      Age |      27 |
|  5 |      3 |     1 | UserName |   Kevin |
|  6 |      3 |     1 |      Age |      18 |
|____|________|_______|__________|_________|

我希望能够将其格式化为以下

 _________________________________ 
|           vUserMeta             |
|_________________________________|
| UserID | orgID | UserName | Age |
|________|_______|__________|_____|
|      1 |     1 |  Scarlet |  23 |
|      2 |     2 |    Diana |  27 | 
|      3 |     1 |    Kevin |  18 |
|________|_______|__________|_____|

1 个答案:

答案 0 :(得分:1)

一种方法是将UserMeta加入自身,如下所示:

SELECT u.userID  AS UserID,
       u.orgID   AS orgID,
       u.keyData AS UserName,
       v.keyData AS Age
FROM   UserMeta u
       JOIN UserMeta v
         ON u.userID = v.userID
            AND u.keyType = 'UserName'
            AND v.keyType = 'Age'