我现在长时间搜索解决这个难题,希望你们中的一些人可以提供帮助。
我有一个看起来像这样的mysql表:
**id | role |**
--------------------
1 | staff |
1 | member |
1 | paying |
2 | staff |
2 | admin |
我在角色中有大约35种不同的数据。 我想要的是角色中每种数据的专栏,所以我最终会得到类似的东西。
**id | staff | member | paying | admin |**
------------------------------------------
1 | staff | member | paying | null |
2 | staff | null | null | admin |
如果有帮助我已经成功使用CASE创建行,但我现在的问题是我得到多个具有相同ID的行。我需要将它们合二为一。到目前为止,我已尝试使用group by但它不起作用。
制作我使用过的coloumns
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
);
并尝试按行ID组合这一行
select id, name, group_concat(fname)
from table
group by id,
答案 0 :(得分:0)
这在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
当然,您可以扩展此查询以包含所有可能的值。