简体   繁体   English

MySQL枢纽分析表,其中各列位于各自的列中

[英]MySQL Pivot a table where columns are in a row of their own

I have a form in a WordPress site using the ARForms plugin which captures names, email and tel numbers, but in a stupid database layout. 我在一个使用ARForms插件的WordPress站点中有一个表单,该表单可以捕获名称,电子邮件和电话号码,但使用的是愚蠢的数据库布局。

I have a MYSQL WordPress table like this: 我有一个像这样的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             |

I am using another plugin Export Report to try and query this table and get a report for all users who have filled out the form. 我正在使用另一个插件“导出报告”来尝试查询此表,并为所有已填写表格的用户获得报告。

I want it in a format like this: 我想要这样的格式:

|ID           |Name              |Surname          |Email            |Telephone
|1            |John              |Smith            |jsmith@mail.com  |555 1234
|2            |Sue               |Brown            |sbrown@mail.com  |555 4321

I have tried the following from looking at a few examples but I keep getting NULL's returned. 我通过查看一些示例尝试了以下方法,但我不断得到NULL的返回值。 I need to filter it out somehow and only pivot where there are values. 我需要以某种方式将其过滤掉,并且只在有值的地方旋转。 NOTE: There are 2 extra fields compared to the example above, InterestedIn and Province: 注意:与上面的示例相比,还有两个额外的字段,InterestedIn和Province:

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 ;

Can anyone please assist... 谁能帮忙...

you are missing, GROUP BY and MAX aggregate function 您丢失了,GROUP BY和MAX聚合函数

you don't need two views, you can do it in one view 您不需要两个视图,可以在一个视图中进行操作

  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

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

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