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 im开发者_如何学JAVAprove my example table.
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
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
精彩评论