So I have a result like this gotten from multiple LEFT JOIN statement where one join is available, the other is null
user_id | country | purpose_id
2 CA NULL
2 NULL 367
2 NG NULL
2 NULL 368
I want to be able to convert this to
user_id | country | purpose_id
2 CA 367
2 NG 368
But I cannot seem to get it with my JOIN statement
SELECT
cs.user_id,
UPPER(country_term.slug),
purpose_term.term_id
FROM
wp_user_services cs
INNER JOIN wp_posts v ON
v.ID = cs.visa_id AND v.post_status = 'publish'
INNER JOIN wp_term_relationships tr ON
tr.object_id = v.ID
INNER JOIN wp_term_taxonomy tt ON
tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy IN('country', 'purpose')
LEFT JOIN wp_terms country_term ON
tt.term_id = country_term.term_id AND tt.taxonomy = 'country'
LEFT JOIN wp_terms purpose_term ON
purpose_term.term_id = tt.term_id AND tt.taxonomy = 'purpose'
WHERE
cs.user_id = 2;
Anyone with ideas?
EDIT:
Fiddle
3
Answers
I solved my problem with nested query after trying to write a PHP script, I realised I used nested loop. It works now.
I would love ideas on how to improve this if there is room for improvement. :)
The relationships here seem to be that a consultant is issued a visa for a country and has purpose(s) for such a visit. Country and purpose(s) can therefore be aggregated eg
note that whilst country is unique to visa a visa could be used many times for different purposes..