0

I included a screenshot of a demo entry to the table. I know how to write an SQL query to select data when all the information is on the same row, but this presents a new challenge to me. I want to select the meta_value result to a meta_key for a user_id only if pw_user_status has the value of approved. I'm not sure how to write this kind of query.

Source data:

enter image description here

1
  • 1
    Can you at least show us what your expected output is? Commented Mar 14, 2017 at 2:52

1 Answer 1

1

You can use a subquery which identifies users having approved status, and then select certain key/value pairs for those users:

SELECT meta_key, meta_value
FROM yourTable t1
WHERE t1.meta_key IN ('nickname', 'first_name', ...) AND
     EXISTS (SELECT 1 FROM yourTable t2
             WHERE t2.user_id = t1.user_id AND
                   t2.meta_key = 'pw_user_status' AND
                   t2.meta_value = 'approved')

Demo here:

Rextester

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.