MySQL combine two columns into rows

Altin

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
drpetermolnar

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql combine rows into columns

From Dev

Combine two mysql rows into one

From Dev

Combine two similar rows into one with new columns

From Dev

Combine rows from two tables with different columns?

From Dev

Combine two similar rows into one with new columns

From Java

MySQL combine two columns into one column

From Java

MySQL SELECT AS combine two columns into one

From Dev

combine two columns in 1 column mysql

From Dev

SQL Select - combine two tables (while transforming rows in columns)

From Dev

Combine rows & columns in dataframes

From Dev

mysql select rows based on two columns

From Dev

Grouping rows via two different columns in MYSQL

From Dev

Combine two columns in SparkR

From Dev

Combine two columns into one

From Dev

mysql get rows between two dates based on two columns

From Dev

SQL combine rows and create columns

From Dev

MS Access: Combine columns and rows to unique rows

From Dev

Efficiently fetching 25 rows with highest sum of two columns (MySQL)

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

Mysql need to select range of rows based on two columns values

From Dev

MySql select rows of unique values between two columns

From Dev

Mysql statement to select distinct rows from two columns

From Dev

MySQL select rows based on a result between two date columns

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

Mysql statement to select distinct rows from two columns

From Dev

I need to compare two different columns in different rows using mysql

From Dev

Combine two consecutive rows in a dataframe

From Dev

Combine two rows into One Column

From Dev

combine files consisting of two columns

Related Related

HotTag

Archive