David Nashash
David Nashash

Reputation: 3

merging two tables but removing duplicates in in a certain column

so I am trying to merge two tables for example

table 1:

name col1 col2
David W C
BOB v w

table 2:

name col1 col2
David o n
Kevin l x

Im trying to merge them such as if I have a duplicate name in table 1 and table 2, ill keep only the one in table 1 so table 3 would be like this

table 3:

left center right
David W C
Bob v w
Kevin l x

if I use union or union all, it creates 2 rows of David but I need only one of them

Upvotes: 0

Views: 96

Answers (2)

aaron_ca
aaron_ca

Reputation: 74

I'm not clear what your goal is, if a match is found in col1/col2 in both tables. but try this:

SELECT
           table1.name
          ,table1.col1
          ,table1.col2
          ,table2.col1
          ,table2.col2

FROM table_1 AS table1
FULL JOIN table_2 AS table2
ON table1.name = table2.name

This will join table_1 and table_2 on the name column It will return null values in table1.col1/col2 and table2.col1/col2 if a match is not found

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270021

I think you want a full join:

select coalesce(t1.name, t2.name), t1.center, t2.right
from table_1 t1 full join
     table_2 t2
     on t1.name = t2.name;

Upvotes: 1

Related Questions