表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查询。
答案 0 :(得分: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
现在,如果您想要动态执行此操作,这意味着您事先不知道要转置的列,那么您应该查看以下文章:
Dynamic pivot tables (transform rows to columns)
您的代码如下所示:
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;