0

表 1:User_Profile

-------------------------------------------------------------------------------------------
User_profile_id   | Profile_form_id |   Email       |   Zipcode   |   Firstname | Lastname
-------------------------------------------------------------------------------------------

1                 | 4               |john@gmail.com |  123456     |  john       | peter 
------------------------------------------------------------------------------------------

-

表 2:Profile_attribute_form

-------------------------------------------------------------------
profile_attribute_id   | Profile_attribute_name |  profile_form_id 
-------------------------------------------------------------------

1                      | Address                |  4
-------------------------------------------------------------------
2                      | Phone Number           |  4
------------------------------------------------------------------
3                      | City                   |  4
-------------------------------------------------------------------

表 3:User_Profile_attribute


User_profile_id        | Profile_attribute_id   |  profile_attribute_value 
--------------------------------------------------------------------------

1                      | 1                      |  23,Times road
--------------------------------------------------------------------------
1                      | 2                      |  9786530874
--------------------------------------------------------------------------
1                      | 3                      |  New York
--------------------------------------------------------------------------

需要的最终结果:

-------------------------------------------------------------------------------------------
User_profile_id|Profile_form_id|Email |Zipcode|Firstname|Lastname|Address|PhoneNumber|City
-------------------------------------------------------------------------------------------

1              |4              |gm.com|123456 |john     |peter   |addr1  |9786530874 |NewY 
-------------------------------------------------------------------------------------------

如何为上述结果构建 mysql 查询。

4

1 回答 1

3

您需要做的是JOIN表格,然后PIVOT是数据。但是 MySQL 没有PIVOT函数,但您可以使用聚合函数和CASE语句来复制该功能。

如果您知道要转换为列的值,则可以使用以下内容:

select up.user_profile_id,
  up.profile_form_id,
  up.email,
  up.zipcode,
  up.firstname,
  up.lastname,
  min(case when paf.Profile_attribute_name= 'Address' 
            then upa.profile_attribute_value end) as Address,
  min(case when paf.Profile_attribute_name= 'Phone Number' 
            then upa.profile_attribute_value end) as PhoneNumber,
  min(case when paf.Profile_attribute_name= 'City' 
            then upa.profile_attribute_value end) as City
from user_profile up
left join Profile_attribute_form paf
  on up.Profile_form_id = paf.Profile_form_id
left join User_Profile_attribute upa
  on paf.profile_attribute_id = upa.Profile_attribute_id
  and up.User_profile_id = upa.User_profile_id
group by up.user_profile_id,
      up.profile_form_id,
      up.email,
      up.zipcode,
      up.firstname,
      up.lastname

SQL Fiddle with Demo

现在,如果您想动态执行此操作,这意味着您事先不知道要转置的列,那么您应该查看以下文章:

动态数据透视表(将行转换为列)

您的代码如下所示:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'min(case when paf.Profile_attribute_name = ''',
      paf.Profile_attribute_name,
      ''' then upa.profile_attribute_value end)  AS ',
      replace(paf.Profile_attribute_name, ' ', '')
    )
  ) INTO @sql
from user_profile up
left join Profile_attribute_form paf
  on up.Profile_form_id = paf.Profile_form_id;

SET @sql 
  = CONCAT('select up.user_profile_id,
              up.profile_form_id,
              up.email,
              up.zipcode,
              up.firstname,
              up.lastname, ', @sql, '
           from user_profile up
           left join Profile_attribute_form paf
             on up.Profile_form_id = paf.Profile_form_id
           left join User_Profile_attribute upa
             on paf.profile_attribute_id = upa.Profile_attribute_id
             and up.User_profile_id = upa.User_profile_id
           group by up.user_profile_id,
                 up.profile_form_id,
                 up.email,
                 up.zipcode,
                 up.firstname,
                 up.lastname');

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

SQL Fiddle with Demo

于 2012-09-14T10:39:19.930 回答