$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.
You could use this:
I’m joining
main_tablewithsecondary_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.