如何根据特定的行值将行转换为列?

时间:2013-02-03 16:56:29

标签: php mysql

我的选择输出如下

| s.no | user_id | user_type | user_group | prefix | fname | mname | lname | suffix |nick_name | company | department | designation_title | industry | dob        | nationality | passport_number | photograph | mobile | email | permanent_address | temporary_address | bbm_p |t_number_arrival | departure_date_time | depart_airlines | flight_number_departure |

|   17 |       0 | Husband   |         23 | sasas  | asd   |       |       |       |           |          |            |                   |          |0000-00-00  |             |                 |            |      0 | asdas |                   | sadx              | asd| 0000-00-00 00:00:00   |                 |                         |
|   18 |       0 | wife      |         23 |        |       |       |       |        |           |         |            |                   |          | 0000-00-00 |             |                 |            |      0 |       |                   |                   |     | 0000-00-00 00:00:00 |                 |                         |
|   19 |       0 | kid1      |         23 |        |       |       |       |        |           |         |            |                   |          | 0000-00-00 |             |                 |            |      0 |       |                   |                   |    | 0000-00-00 00:00:00 |                 |                         |
|   20 |       0 | kid2      |         23 |        |       |       |       |        |           |         |            |                   |          | 0000-00-00 |             |                 |            |      0 |       |                   |                   |     | 0000-00-00 00:00:00 |                 |                         |
|   21 |       0 | kid3      |         23 |        |       |       |       |        |           |         |            |                   |          | 0000-00-00 |             |                 |            |      0 |       |                   |                   |    | 0000-00-00 00:00:00 |                 |                         |
|   22 |       0 | kid4      |         23 |        |       |       |       |        |           |         |            |                   |          | 0000-00-00 |             |                 |            |      0 |       |                   |                   |     | 0000-00-00 00:00:00 |                 |                         |
|   23 |       0 | Husband   |         24 | sasas  | asd   |       |       |        |           |         |            |                   |          | 0000-00-00 |             |                 |            |      0 | asdas |                   | sadx              | asd| 0000-00-00 00:00:00 |                 |                         |
|   24 |       0 | wife      |         24 |        |       |       |       |        |           |         |            |                   |          | 0000-00-00 |             |                 |            |      0 |       |                   |                   |    | 0000-00-00 00:00:00 |                 |                         |
|   25 |       0 | kid1      |         24 |        |       |       |       |        |           |         |            |                   |          | 0000-00-00 |             |                 |            |      0 |       |                   |                   |

我需要转换如下

 | husband | wife | kid1 | kid2 | kid3 | kid4

   sasas  |       |      |       |      |
    asd   |       |      |       |      |

如何修改我的选择查询以对其下的记录进行分组 丈夫,妻子,小孩1,小孩2,小孩3和小孩4?

1 个答案:

答案 0 :(得分:1)

您的问题并不完全清楚,但似乎您希望取消忽略现有列,然后转移 user_type列中的值。

如果是这种情况,那么您将需要使用UNION ALL取消数据的转移,然后应用带有CASE表达式的聚合函数来进行转动以获得最终结果:

select user_id, 
  user_group,
  col_name,
  max(case when user_type = 'Husband' then value end) as Husband,
  max(case when user_type = 'wife' then value end) as Wife,
  max(case when user_type = 'kid1' then value end) as Kid1,
  max(case when user_type = 'kid2' then value end) as Kid2,
  max(case when user_type = 'kid3' then value end) as Kid3,
  max(case when user_type = 'ki4=d4' then value end) as Kid4
from
(
  select user_id, user_type, user_group,
      'prefix' as col_name, prefix as value
  from yourtable
  union all
  select user_id, user_type, user_group,
      'fname' as col_name, fname as value
  from yourtable
  union all
  select user_id, user_type, user_group,
      'mname' as col_name, mname as value
  from yourtable
  union all
  select user_id, user_type, user_group,
      'lname' as col_name, lname as value
  from yourtable
  union all
  select user_id, user_type, user_group,
      'suffix' as col_name, suffix as value
  from yourtable
  union all
  select user_id, user_type, user_group,
      'nick_name' as col_name, nick_name as value
  from yourtable
  union all
  select user_id, user_type, user_group,
      'company' as col_name, company as value
  from yourtable
  union all
  select user_id, user_type, user_group,
      'department' as col_name, department as value
  from yourtable
) src
group by user_id, user_group, col_name

SQL Fiddle with Demo。您的示例数据没有很多值,但结果与此类似:

| USER_ID | USER_GROUP |   COL_NAME | HUSBAND |   WIFE |   KID1 |   KID2 |   KID3 |   KID4 |
--------------------------------------------------------------------------------------------
|       0 |         23 |    company |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         23 | department |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         23 |      fname |     asd | (null) | (null) | (null) | (null) | (null) |
|       0 |         23 |      lname |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         23 |      mname |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         23 |  nick_name |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         23 |     prefix |   sasas | (null) | (null) | (null) | (null) | (null) |
|       0 |         23 |     suffix |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         24 |    company |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         24 | department |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         24 |      fname |     asd | (null) | (null) | (null) | (null) | (null) |
|       0 |         24 |      lname |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         24 |      mname |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         24 |  nick_name |  (null) | (null) | (null) | (null) | (null) | (null) |
|       0 |         24 |     prefix |   sasas | (null) | (null) | (null) | (null) | (null) |
|       0 |         24 |     suffix |  (null) | (null) | (null) | (null) | (null) | (null) |

注意:我添加了其他列以进行取消删除,但如果您只想要fname,则只需在子查询中包含这些列

编辑#1,如果您需要根据原始表中的列保留数据的顺序,则可以使用排序顺序向UNION ALL个查询添加一列。然后,您可以在ORDER BY中使用该列。所以查询将是:

select user_id, 
  user_group,
  col_name,
  max(case when user_type = 'Husband' then value end) as Husband,
  max(case when user_type = 'wife' then value end) as Wife,
  max(case when user_type = 'kid1' then value end) as Kid1,
  max(case when user_type = 'kid2' then value end) as Kid2,
  max(case when user_type = 'kid3' then value end) as Kid3,
  max(case when user_type = 'ki4=d4' then value end) as Kid4
from
(
  select user_id, user_type, user_group,
      'prefix' as col_name, prefix as value
      , 1 as sortorder
  from yourtable
  union all
  select user_id, user_type, user_group,
      'fname' as col_name, fname as value
      , 2 as sortorder
  from yourtable
  union all
  select user_id, user_type, user_group,
      'mname' as col_name, mname as value
      , 3 as sortorder
  from yourtable
  union all
  select user_id, user_type, user_group,
      'lname' as col_name, lname as value
      , 4 as sortorder
  from yourtable
  union all
  select user_id, user_type, user_group,
      'suffix' as col_name, suffix as value
      , 5 as sortorder
  from yourtable
  union all
  select user_id, user_type, user_group,
      'nick_name' as col_name, nick_name as value
      , 6 as sortorder
  from yourtable
  union all
  select user_id, user_type, user_group,
      'company' as col_name, company as value
      , 7 as sortorder
  from yourtable
  union all
  select user_id, user_type, user_group,
      'department' as col_name, department as value
      , 8 as sortorder
  from yourtable
) src
group by user_id, user_group, col_name
order by user_group, sortorder

请参阅SQL Fiddle with Demo