简体   繁体   English

如何获得具有多个表关系的单行记录?

[英]How to get a single row of record with multiple relationship of tables?

Table 1 : User_Profile 表1:User_Profile

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

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

- --

Table 2 : Profile_attribute_form 表2:Profile_attribute_form

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

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

Table 3 : User_Profile_attribute 表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
--------------------------------------------------------------------------

Needed Final Result : 所需的最终结果:

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

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

How to i build the mysql query for the above result. 我如何建立以上结果的mysql查询。

You are need to do is JOIN the tables and then PIVOT the data. 您需要做的是JOIN表,然后将数据PIVOT However MySQL does not have a PIVOT function but you can replicate the functionality using an aggregate function and a CASE statement. 但是,MySQL没有PIVOT函数,但是您可以使用聚合函数和CASE语句复制该功能。

If you know the values to turn into columns then you can use the following: 如果您知道要转换为列的值,则可以使用以下内容:

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

see SQL Fiddle with Demo 参见带有演示的SQL Fiddle

Now if you want to perform this dynamically, meaning you do not know ahead of time the columns to transpose, then you should review the following article: 现在,如果您要动态执行此操作,这意味着您不知道要转置的列的提前时间,那么您应该查看以下文章:

Dynamic pivot tables (transform rows to columns) 动态数据透视表(将行转换为列)

Your code would look like this: 您的代码如下所示:

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;

see SQL Fiddle with Demo 参见带有演示的SQL Fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM