2

我正在试用“PHP 登录和用户管理”插件,该插件允许在用户个人资料中使用自定义字段。
但是,由于某种原因,这是在单独的表上实现的,因此例如,为了提取“电话号码”,我必须从 login_users 获取我的 user_id,进入 login_profile_fields 表,找到正确的行,提取 id 和标签,并在 login_profiles 中找到 和 的id=pfield_iduser_id = user_id

我正在尝试编写一个 SQL 查询以在最后显示以下信息:

姓名 | 电子邮件 | 电话号码 | 邮政编码 | 部门 | 技能 | 经理邮箱 | 公司代码 | 地位

在 login_profiles 中选择的值是 X (例如:“列出所有状态码为 1 的用户的上述信息”)

我有办法做到这一点吗?

或者,有没有一种方法可以使用 login_profiles 中的值自动填充 login_profiles 表,就像这样?

login_profiles

p_id | pfield_id | user_id | profile_value | Phone Number | Zip Code | ...
1    | 1         | 1       | 18005551212   | {Select profile_value from login_profiles where user_id=user_id and pfield_id=1} | {Select profile_value from login_profiles where user_id=user_id and pfield_id=2} | ...

这是我当前的表格:

login_profile_fields

id | section   |   type     |   label       |
1  | User Info | text_input | Phone Number  |
2  | User Info | text_input | Zip Code      |
3  | Work Info | text_input | Department    |
4  | Work Info | text_input | Skills        |
5  | Work Info | text_input | Manager Email |
6  | Work Info | text_input | Company Code  |
7  | Work Info | checkbox   | Status        |

login_profiles

p_id | pfield_id | user_id | profile_value |
1    | 1         | 1 | 18005551212 | 
2    | 2         | 1 | 90210 | 
3    | 3         | 1 | Marketing |
4    | 4         | 1 | Segmentations and surveys |
5    | 5         | 1 | theboss@company.com |
6    | 6         | 1 | COMP1 |
7    | 7         | 1 | 1 |
1    | 1         | 2 | 18007771234 | 
2    | 2         | 2 | 90218 | 
3    | 3         | 2 | CEO |
4    | 4         | 2 | Business strategy |
5    | 5         | 2 | theboard@company.com |
6    | 6         | 2 | COMP1 |
7    | 7         | 2 | 1 |

登录用户

user_id| name          | email |
     1 | Michael Bluth | worker@company.com |
     2 | George Bluth  | theboss@company.com |

我不是受过培训的 MYSQL 人,但我正在尽我所能学习,所以非常感谢任何建议!

4

1 回答 1

2

您要加入外键关系上的表,然后可以使用带有 CASE 表达式的聚合函数将行转换为列:

select u.name,
  u.email,
  max(case when f.label = 'Phone Number' then p.profile_value end) PhoneNumber,
  max(case when f.label = 'Zip Code' then p.profile_value end) ZipCode,
  max(case when f.label = 'Department' then p.profile_value end) Department,
  max(case when f.label = 'Skills' then p.profile_value end) Skills,
  max(case when f.label = 'Manager Email' then p.profile_value end) ManagerEmail,
  max(case when f.label = 'Company Code' then p.profile_value end) CompanyCode,
  max(case when f.label = 'Company Code' then p.profile_value end) Status
from login_profile_fields f
left join login_profile p
  on f.id = p.pfield_id
left join login_users u
  on p.user_id = u.user_id
group by u.name, u.email;

请参阅带有演示的 SQL Fiddle

如果您有未知数量的值,那么您可能需要使用准备好的语句来生成动态 SQL 来执行:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(CASE WHEN f.label = ''',
      label,
      ''' THEN p.profile_value END) AS `',
      label, '`'
    )
  ) INTO @sql
FROM login_profile_fields;

SET @sql 
  = CONCAT('SELECT u.name,
              u.email, ', @sql, ' 
            from login_profile_fields f
            left join login_profile p
              on f.id = p.pfield_id
            left join login_users u
              on p.user_id = u.user_id
            group by u.name, u.email');

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

请参阅带有演示的 SQL Fiddle

于 2013-06-24T15:53:59.620 回答