Merging multiple rows into one row and multiple columns on mysql

后端 未结 3 1095
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 07:45

I am working in MYSQL and need to extract user data to be pulled into a view. I will be using the data in an email client, so I cannot do this in the app layer.

The

3条回答
  •  长发绾君心
    2020-12-30 07:55

    Here is an another solution that doesn't need additional join operations, instead, we make use of group by statement along with case statement.

    select um.user_id,
           max(case when um.meta_key ='first_name' then um.meta_data end) AS first_name,  
           max(case when um.meta_key ='last_name' then um.meta_data end) AS last_name  ,
           max(case when um.meta_key ='email' then um.meta_data end) AS email
    from wp_usermeta um 
    group by user_id;
    

    Note the max function is just for making them into one row, you can use min as well.

    Check SQL Fiddler Demo here

提交回复
热议问题