我在WordPress网站上有一个表格,使用ARForms插件捕获姓名,电子邮件和电话号码,但是采用愚蠢的数据库布局。
我有一个像这样的MYSQL WordPress表:
|id |entry_value |field_id |entry_ID |
|1 |John |74 |1 |
|2 |Smith |75 |1 |
|3 |555 1234 |76 |1 |
|4 |jsmith@mail.com |77 |1 |
|5 |Sue |74 |2 |
|6 |Brown |75 |2 |
|7 |555 4321 |76 |2 |
|8 |sbrown@mail.com |77 |2 |
我正在使用其他插件导出报告来尝试查询此表,并为填写表单的所有用户获取报告。
我希望它采用以下格式:
|ID |Name |Surname |Email |Telephone
|1 |John |Smith |jsmith@mail.com |555 1234
|2 |Sue |Brown |sbrown@mail.com |555 4321
我已经尝试了以下几个示例,但我一直收到NULL返回。我需要以某种方式过滤掉它,并且只在有价值的地方进行调整。注意:与上面的示例相比,还有2个额外字段,InterestedIn和省:
create view users_subscribed as (
select
a.entry_id,
a.field_id,
case when field_id = '74' then entry_value end as FirstName,
case when field_id = '75' then entry_value end as LastName,
case when field_id = '76' then entry_value end as Email,
case when field_id = '78' then entry_value end as Phone,
case when field_id = '79' then entry_value end as InterestedIn,
case when field_id = '81' then entry_value end as Province
from ch_arf_entry_values a
);
create view users_subscribed_extended_pivot as (
select
entry_id as ID,
FirstName,
LastName,
Email,
Phone,
InterestedIn,
Province
from users_subscribed
group by entry_id
);
SELECT * FROM users_subscribed_extended_pivot ;
任何人都可以协助......
答案 0 :(得分:1)
你缺少,GROUP BY和MAX聚合函数
您不需要两个视图,您可以在一个视图中执行此操作
select
a.entry_id,
MAX(case when field_id = 74 then entry_value end) as FirstName,
MAX(case when field_id = 75 then entry_value end) as LastName,
MAX(case when field_id = 76 then entry_value end) as Email,
MAX(case when field_id = 78 then entry_value end) as Phone,
MAX(case when field_id = 79 then entry_value end) as InterestedIn,
MAX(case when field_id = 81 then entry_value end) as Province
from ch_arf_entry_values a
GROUP BY a.entry_id