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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:45:30+00:00 2026-06-14T08:45:30+00:00

$referrer is the only defined variable. main_table holds the relationship between users and referrers.

  • 0

$referrer is the only defined variable.

main_table holds the relationship between users and referrers. a referrer can have many users.

user_id  | referrer
1        | seller
2        | abother seller
3        | another seller
4        | seller

secondary_table holds user_id, meta_key and meta_value with last_name and first_name being meta_keys.

user_id  | meta_key   | meta_value
1        | first_name | John
1        | last_name  | Doe
4        | first_name | Betty
4        | last_name  | Boo

I need to merge the following 3 queries

SELECT user_id FROM main_table WHERE referrer = $referrer

SELECT meta_value FROM secondary_table WHERE user_id = $user_id AND meta_key = first_name

SELECT meta_value FROM secondary_table WHERE user_id = $user_id AND meta_key = last_name

into one query so I can use it with $results = $wpdb->get_results, then asort() the $results by last_name to have an alphabetically ordered output and echo $results with a foreach like

foreach ($results as $result) {
  echo $result->user_id.' '.$result->first_name.' '.$result->last_name;
}

If $referrer == “seller” the output should look like this:

4 Betty Boo
1 John Doe

How should that single query look like?

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-06-14T08:45:31+00:00Added an answer on June 14, 2026 at 8:45 am

    You could use this:

    select
      main_table.user_id,
      concat(s1.meta_value, ' ', s2.meta_value) as name
    from
      main_table left join secondary_table s1
      on main_table.user_id = s1.user_id and s1.meta_key = 'first_name'
      left join secondary_table s2
      on main_table.user_id = s2.user_id and s2.meta_key = 'last_name'
    where
      referrer = $referrer
    

    I’m joining main_table with secondary_table twice, the first time filtered on the first name, the second time filtered on the last name. Then to get the name you just have to concat the first meta_value with the second.

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

Sidebar

Related Questions

I have a simple referrers table with three columns: Referrer //string name of partner
I want to redirect back to referrer url in playframework and I have tried
The sites of my users collect referrer data about their users and store it
I have a php script that check if the referrer has been cleared after
My goal is to have user-uploaded files (mostly PDF) available for download only via
We only want users from a specific website to use our services. Is there
How can I apply a rule with mod_rewrite only if a file is directly
I have defined a rewrite URL in http.conf to append a parameter to the
I have a small widget. I want only a few websites to embed my
I want to save everything that can be used for statistics, such as referrer,

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.