1

I need to join 2 columns into only one as follows.

enter image description here

2
  • 2
    Try using UNION? Commented Jan 10, 2018 at 18:57
  • Please read postgresql-performance then edit your question and provide the missing information. Commented Jan 10, 2018 at 19:31

4 Answers 4

3

You could use union

If both the columns are in the same table

select connection from my_table
union 
select followed_id from my_table

or change the table name if are in different tables

select connection from my_table1
union 
select followed_id from my_table2
Sign up to request clarification or add additional context in comments.

Comments

2
select * from (
    select follower_id from T
    union
    select followed_id from T
)

Comments

1

If the values are in one table then use cross join with lateral. It uses just one table scan.

SELECT v.*
FROM   table, LATERAL (
   VALUES
      (follower_id )
    , (followed_id)  -- data types must be compatible
   ) v ("connections")

similar question: Select distinct on multiple columns

2 Comments

How is this different than UNION ?
@JuanCarlosOropeza I believe it uses just one table scan
0

Union works, I would also add "order by"

select a.follower_id "Connections" 
from table_name a 
union 
select b.followed_id
from table_name b
order by "Connections"

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.