0

I'm doing this query

SELECT 
ub_properties.title, 
ub_level_two.level_two AS level_two, 
ub_autocomplete.level_two AS level_two_extra, 
ub_utility.utility  AS utility, 
ub_autocomplete.utility AS utility_extra 
FROM ub_properties 
LEFT JOIN ub_meta ON ub_properties.post_id=ub_meta.post_id 
LEFT JOIN ub_level_two ON ub_meta.level_two_id=ub_level_two.id 
LEFT JOIN ub_utility ON ub_meta.utility_id=ub_utility.id 
LEFT JOIN ub_autocomplete ON ub_meta.autocomplete_id=ub_autocomplete.id 
WHERE ub_properties.user_id=1

and getting this result:

enter image description here

Notice columns level_two and level_two_extra -they should be in the same column. So in essence I need to to combine the results of these two columns in one column (will result in more rows in result set).

I can't use CONCAT here because for technical reasons I want them each in its own row.

Any suggestion to point me in the right direction will be much appreciated. Been searching all over the place with no success so far.

---- Edit, SOLUTION ---- ( thanks @drpetermolnar )

SELECT 
ub_properties.title, 
ub_level_two.level_two, 
ub_utility.utility
FROM ub_properties 
LEFT JOIN ub_meta ON ub_properties.post_id=ub_meta.post_id 
LEFT JOIN ub_level_two ON ub_meta.level_two_id=ub_level_two.id 
LEFT JOIN ub_utility ON ub_meta.utility_id=ub_utility.id 
WHERE ub_properties.user_id=1
UNION
SELECT 
ub_properties.title, 
ub_autocomplete.level_two, 
ub_autocomplete.utility 
FROM ub_properties 
LEFT JOIN ub_meta ON ub_properties.post_id=ub_meta.post_id 
LEFT JOIN ub_autocomplete ON ub_meta.autocomplete_id=ub_autocomplete.id
WHERE ub_properties.user_id=1
2
  • Why you cant use CONCAT()? pls explain. Commented Nov 11, 2015 at 9:03
  • because at the next step I will be wanting to get the IDs as well as the text in those fields (that I'm getting now). Which means I will need to get not only one field from the ub_level_two table, but probably 2-3 fields and combine them with 2-3 corresponding fields from ub_autocomplete table. So yep, right now I can use CONCAT, but at a later step I will need to combine them into rows anyways. That's how I see it, maybe that's wrong but... :) Commented Nov 11, 2015 at 13:41

1 Answer 1

1

Use the UNION keyword https://dev.mysql.com/doc/refman/5.7/en/union.html Write two SELECT statements that only differ in one place: the first uses level_two, the second uses level_two_extra. Combine with UNION.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.