I have below SQL command that works to get user_meta details in WordPress. This works fine to get the data in one table and one row per user info with info for each user in each column but I’m just trying to find out how this works. Can anyone be kind enough to explain below?:
SELECT
u.id, u.user_login,
MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END) AS title,
MIN(CASE m.meta_key WHEN 'first_name' THEN m.meta_value END) AS first_name,
MIN(CASE m.meta_key WHEN 'last_name' THEN m.meta_value END) AS last_name,
MIN(CASE m.meta_key WHEN 'suburb' THEN m.meta_value END) AS phone,
MIN(CASE m.meta_key WHEN 'state' THEN m.meta_value END) AS state,
MIN(CASE m.meta_key WHEN 'country' THEN m.meta_value END) AS country,
MIN(CASE m.meta_key WHEN 'postcode' THEN m.meta_value END) AS postcode,
MIN(CASE m.meta_key WHEN 'contact_no' THEN m.meta_value END) AS contact_no,
MIN(CASE m.meta_key WHEN 'email' THEN m.meta_value END) AS email,
MIN(CASE m.meta_key WHEN 'occupation' THEN m.meta_value END) AS occupation,
MIN(CASE m.meta_key WHEN 'workplace' THEN m.meta_value END) AS workplace,
MIN(CASE m.meta_key WHEN 'maternitybg' THEN m.meta_value END) AS maternitybg,
MIN(CASE m.meta_key WHEN 'trainingdate' THEN m.meta_value END) AS trainingdate,
MIN(CASE m.meta_key WHEN 'traininglocation' THEN m.meta_value END) AS traininglocation,
MIN(CASE m.meta_key WHEN 'coltraining' THEN m.meta_value END) AS coltraining,
MIN(CASE m.meta_key WHEN 'trainingyear' THEN m.meta_value END) AS trainingyear,
MIN(CASE m.meta_key WHEN 'coltraining' THEN m.meta_value END) AS coltraining,
MIN(CASE m.meta_key WHEN 'isinstructor' THEN m.meta_value END) AS isinstructor,
MIN(CASE m.meta_key WHEN 'gender' THEN m.meta_value END) AS gender,
MIN(CASE m.meta_key WHEN 'idf_indig_tsi' THEN m.meta_value END) AS idf_indig_tsi,
MIN(CASE m.meta_key WHEN 'idf_ct_ld' THEN m.meta_value END) AS idf_ct_ld,
MIN(CASE m.meta_key WHEN 'comments' THEN m.meta_value END) AS comments
FROM wp_users u
LEFT JOIN wp_usermeta m ON u.ID = m.user_id
AND m.meta_key IN ('title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments')
GROUP BY u.ID
Thanks for your help!
Not sure what part you want explained but anyway, here we go
This part is about specifying what columns you want:
Followed by the part where you include the source tables:
By using
left joinyou make sure that you will get rows fromwp_userseven if there are no matches inwp_usermeta.ON u.ID = m.user_idconnect the rows betweenwp_usersandwp_usermeta.Apparently,
wp_usermetais key-value table and this limits the keys you are interested in.Finally there is group by clause that will make the output one row for each distinct value of
u.ID.There is a little trick in the field list as well using a case statement.
First you get the value
m.meta_valueif them.meta_keyequals'title', otherwise the value will bynull.If there are more than one
'title'keys inwp_usermetafor a given user you will get the min() value. Title ‘Bus driver’ will be picked before title ‘Cab driver’.And at last you give the column retrieved a column alias
title.