Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6609415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:42:55+00:00 2026-05-25T19:42:55+00:00

I have below SQL command that works to get user_meta details in WordPress. This

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:42:55+00:00Added an answer on May 25, 2026 at 7:42 pm

    Not sure what part you want explained but anyway, here we go

    This part is about specifying what columns you want:

    SELECT
        u.id, u.user_login,
    

    Followed by the part where you include the source tables:

    FROM wp_users u
      LEFT JOIN wp_usermeta m ON u.ID = m.user_id
    

    By using left join you make sure that you will get rows from wp_users even if there are no matches in wp_usermeta. ON u.ID = m.user_id connect the rows between wp_users and wp_usermeta.

    Apparently, wp_usermeta is key-value table and this limits the keys you are interested in.

    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')
    

    Finally there is group by clause that will make the output one row for each distinct value of u.ID.

    GROUP BY u.ID
    

    There is a little trick in the field list as well using a case statement.

    MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END) AS title,
    

    First you get the value m.meta_value if the m.meta_key equals 'title', otherwise the value will by null.

    CASE m.meta_key WHEN 'title' THEN m.meta_value END
    

    If there are more than one 'title' keys in wp_usermeta for a given user you will get the min() value. Title ‘Bus driver’ will be picked before title ‘Cab driver’.

    MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END)
    

    And at last you give the column retrieved a column alias title.

    MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END) AS title,
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn more about regular expressions I have one below that
I have a script that appends some rows to a table. One of the
SQL Server (2005/2008) Each of the below statements have the same result. Does anyone
I have an MS SQL function that is called with the following syntax: SELECT
So I have an SQL dump file that needs to be loaded using mysql_query().
I have a table that currently is a long list of devices and information
I have a sql query shown below i want to use the variables streetname,
I have this code UPDATE OPENQUERY (db,'SELECT * FROM table WHERE ref = ''+
I have an existing table in an Oracle 10gR2 that I added a XMLTYPE
I have created this function GetSubName that I need to return the name that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.