0

i would like to export all users with user_metas.

Right now my sql looks like that

SELECT 
    u.ID, 
    u.user_email,
    u.display_name,
    um1.meta_value AS first_name,
    um2.meta_value AS last_name,
    um3.meta_value AS phone_number,
    um4.meta_value AS salutation,um5.meta_value AS title
FROM wp_users u 
LEFT JOIN wp_usermeta  um1 ON u.ID = um1.user_id
LEFT JOIN wp_usermeta  um2 ON u.ID = um2.user_id
LEFT JOIN wp_usermeta  um3 ON u.ID = um3.user_id
LEFT JOIN wp_usermeta  um4 ON u.ID = um4.user_id
LEFT JOIN wp_usermeta  um5 ON u.ID = um5.user_id
WHERE um1.meta_key = 'first_name'
  AND um2.meta_key = 'last_name'
  AND um3.meta_key = 'phone_number'
  AND um4.meta_key = 'salutation'
  AND um5.meta_key = 'title'

The problem is I would like to get all user and not only those with the meta_keys specified above.

How would I do that?

1
  • Can you remove the WHERE clause?
    – jgraup
    Commented Nov 15, 2016 at 14:52

1 Answer 1

2
SELECT
  u.ID,
  u.user_email,
  u.display_name,
  um1.meta_value AS first_name,
  um2.meta_value AS last_name,
  um3.meta_value AS phone_number,
  um4.meta_value AS salutation,
  um5.meta_value AS title
FROM wp_users u
  LEFT JOIN wp_usermeta um1 ON u.ID = um1.user_id AND um1.meta_key = 'first_name'
  LEFT JOIN wp_usermeta um2 ON u.ID = um2.user_id AND um2.meta_key = 'last_name'
  LEFT JOIN wp_usermeta um3 ON u.ID = um3.user_id AND um3.meta_key = 'phone_number'
  LEFT JOIN wp_usermeta um4 ON u.ID = um4.user_id AND um4.meta_key = 'salutation'
  LEFT JOIN wp_usermeta um5 ON u.ID = um5.user_id AND um5.meta_key = 'title'
;

that's the right one. thanks jgraup for the hint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.