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 3944324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:53:13+00:00 2026-05-20T00:53:13+00:00

I am doing some database queries on WordPress. I am joining the usermeta table

  • 0

I am doing some database queries on WordPress. I am joining the usermeta table and users table.

the users table has a single row for each user.
the usermeta table has multiple rows for each user as the “meta_keys” (categories of meta data of each user) can contain all kind of information.

In a query I now would like to create a result set with only one row per user and with additional columns for selected meta_key values defined.

e.g. having an additional columns with the nickname – it should show the content of usermeta.meta_value when usermeta.meta_keys = ‘nickname’.

Here’s my current query with the unwanted duplication of rows

SELECT 
wusers.ID,
wusers.user_login, 
wusers.display_name,
wmeta.meta_key,
wmeta.meta_value,
wmeta.user_id   
FROM
  $wpdb->users wusers
  INNER JOIN $wpdb->usermeta wmeta ON wusers.ID = wmeta.user_id
WHERE 1 = 1
AND wmeta.meta_key = 'nickname'
OR wmeta.meta_key = 'description'
OR wmeta.meta_key = 'userphoto_thumb_file'

Is there any MySQL magic I can use to do this and turn data of certain the rows to new “virtual” columns?

  • 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-20T00:53:13+00:00Added an answer on May 20, 2026 at 12:53 am

    What you seek is commonly called a crosstab query:

    Select U.Id
        , Min( Case When M.meta_key = 'nickname' Then M.meta_value End ) As nickname
        , Min( Case When M.meta_key = 'description' Then M.meta_value End ) As description
        , Min( Case When M.meta_key = 'userphoto_thumb_file' Then M.meta_value End ) As userphoto_thumb_file
    From users As U
        Join usermeta As M
            On M.user_id = U.id
    Group By U.id
    

    It should be noted that you can only do this with static SQL and static columns. The SQL langugage itself was not really designed for dynamic column generation. To dynamically assemble the columns, you will need to dynamically assemble the query (aka dynamic SQL) in your middle-tier code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, I'm doing some queries to a MySQL database that involves some user-input..
I am doing some pre-processing on my database. Basically two queries, one is INSERT
I'm doing some queries in Python on a large database to get some stats
I have an access database which has some sql queries loaded into it. I
I am doing some queries from a database to make graphs, and a graph
I am doing some big queries on my database with Hibernate and I sometimes
I am doing some research about how to access data in a UniData database
I have compared two queries which fetch some fairly large data from a database
I have been using LINQPad to prototype some queries against a SQL 2000 database,
I'm using a Database to run some queries and was thinking to optimize stuff

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.