简体   繁体   English

mysql:从一列创建多个列

[英]mysql: create multiple columns from one column

I search a long time now to solve this puzzle, hopefully some of you can help. 我已经搜寻了很长时间以解决这个难题,希望其中一些人能提供帮助。

I have a mysql table looking like this: 我有一个如下所示的mysql表:

**id | role     |**  
--------------------
   1 | staff    |  
   1 | member   |  
   1 | paying   |  
   2 | staff    |  
   2 | admin    |  

I have around 35 different kinds of data in role. 我有大约35种不同类型的数据。 What i want is a column for every kind of data in role so i will end up with something like this. 我想要的是针对角色中的每种数据的列,因此我最终将得到这样的结果。

**id | staff | member | paying | admin |**  
------------------------------------------
   1 | staff | member | paying | null  |  
   2 | staff | null   | null   | admin |  

If it helps i have already succeed in making the rows using CASE, but my problem now is that I get multiple rows with the same id. 如果有帮助,我已经成功使用CASE制作了行,但是现在的问题是我得到了多个具有相同ID的行。 And I need to combine them into one. 我需要将它们合并为一个。 So far i have tried using group by but it is not working. 到目前为止,我已经尝试使用group by,但是它不起作用。

to make the coloumns i used this 为了使我用这种语言

create table ahbtest2 as (
  select
    ahbtest1.*,
    case when f_name = "newsletter" then f_name end as f_newsletter,
    case when f_name = "staff" then f_name end as f_staff,
    case when f_name = "Musician" then f_name end as f_musician,
    case when f_name = "No_membership" then f_name end as f_no_membership,
    case when f_name = "Beirut" then f_name end as f_beirut,
    case when f_name = "VISA" then f_name end as f_visa,
    case when f_name = "3wc" then f_name end as f_3wc,
    case when f_name = "Intl_media" then f_name end as f_intl_media,
    case when f_name = "VIP" then f_name end as f_vip,
    case when f_name = "Music_industry" then f_name end as f_music_industry,
    case when f_name = "senegal" then f_name end as f_senegal,
    case when f_name = "Music_Freedom_Day" then f_name end as f_music_freedom_day,
    case when f_name = "Scholar/student" then f_name end as f_scholar_student,
    case when f_name = "Anniversary_booklet" then f_name end as f_anniversary_booklet,
    case when f_name = "Music_industry" then f_name end as f_music_industry,
    case when f_name = "Old_membership" then f_name end as f_old_membership,
    case when f_name = "2wc" then f_name end as f_2wc,
    case when f_name = "Advisory_Board" then f_name end as f_advisory_board,
    case when f_name = "Danish_media" then f_name end as f_danish_media,
    case when f_name = "Business_contacts" then f_name end as f_business_contacts,
    case when f_name = "Latest_Report" then f_name end as f_latest_report,
    case when f_name = "Press_Alert" then f_name end as f_press_alert,
    case when f_name = "Funding/sponsor" then f_name end as f_funding_sponsor,
    case when f_name = "Diverse/Contacts" then f_name end as f_diverse_contacts,
    case when f_name = "Honorary_membership" then f_name end as f_honorary_membership,
    case when f_name = "Supporting_Membership" then f_name end as f_supporting_membership,
    case when f_name = "GA_2008" then f_name end as f_ga_2008,
    case when f_name = "Stringer" then f_name end as f_stringer,
    case when f_name = "Seminar" then f_name end as f_seminar,
    case when f_name = "Ambassador" then f_name end as f_ambassador,
    case when f_name = "1wc" then f_name end as f_1wc,
    case when f_name = "Evaluation Contacts" then f_name end as f_evaluation_contacts,
    case when f_name = "OIC_APPEAL" then f_name end as f_oic_appeal,
    case when f_name = "Paying membership" then f_name end as f_paying_membership,
    case when f_name = "Executive committee" then f_name end as f_executive_committee,
    case when f_name = "Institutional membership" then f_name end as f_institutional_membership
  from ahbtest1

);

and in trying combine the rows by id, this one 并尝试通过ID合并行,这一个

select id, name, group_concat(fname)
  from table
  group by id,

This would be long at MySQL level, but still possible: 这在MySQL级别上会很长,但仍然可能:

select id, staff, member, ccc from roles
left join (select id, role as staff from roles where role='staff') staff using (id)
left join (select id, role as member from roles where role='member') member using (id)
left join (select id, role as paying from roles where role='paying') paying using (id)
group by id

Of course, you would extend this query to include all possible values. 当然,您可以扩展此查询以包括所有可能的值。

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

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