我希望将一组用户和用户元数据查询到一个表中。我可以通过创建多个临时表和一个数据透视来做到这一点:
create temporary table meta
select u.ID, u.user_login, meta_key, meta_value from wp_users u
inner join wp_usermeta m on m.user_id = u.ID;
create temporary table users
select ID, user_login,
MAX(CASE WHEN meta_key = \'first_name\' THEN meta_value END) as first_name,
MAX(CASE WHEN meta_key = \'middle_name\' THEN meta_value END) as middle_name,
MAX(CASE WHEN meta_key = \'last_name\' THEN meta_value END) as last_name,
MAX(CASE WHEN meta_key = \'university_program\' THEN meta_value END) as university_program,
MAX(CASE WHEN meta_key = \'wp_capabilities\' THEN meta_value END) as wp_capabilities
from meta
group by ID, user_login;
drop temporary table meta;
select * from users
where wp_capabilities like \'%um_student%\';
drop temporary table users;
但是,是否有更好、更短的方法来运行此查询,而无需创建临时表或透视?