Francois Zbinden
Francois Zbinden

Reputation: 189

Join two Tables, ignore records from first table if exists in second

How to join to tables, if I prefer to get records only from the second table? If no second exists, take record the of the first table.

Table A         Table B         Result
pid,name,type   pid,name,type   pid,name,type
1,Anna,null     1,Anna,100      "1,Anna,100"
2,Bea,null      -               "2,Bea,Null"
3,Caro,null     3,Caro,100      "3,Caro,100"    
                3,Caro,200      "3,Caro,200"

Edit: I tried to improve my example table.

Upvotes: 1

Views: 2924

Answers (2)

gbn
gbn

Reputation: 432261

SELECT
   ISNULL(A.column1, B.column1),  --perhaps name
   ISNULL(A.column2, B.column2)  --perhaps id
FROM
   TableA A
   FULL OUTER JOIN
   TableB B ON A.column1 = B.column1  --assume name is matching column

Edit, I'm assuming you'd have no row in tableA, and "Dave, 6" in tableB. Otherwise, Cybernate's left join is enough

Upvotes: 5

Chandu
Chandu

Reputation: 82903

You can use a Left Join, I assume the column names of TableB are Name and ID respectively.

SELECT tableA.Name, tableB.Id
  FROM tableA LEFT JOIN tableB
    ON tableA.Name = tableB.Name

Upvotes: 3

Related Questions