I have 2 tables Test1 and Test1Update with following structure
Test1
id name address
Test1Update
id name address
here Test1 is old table and Test1Update is just replica of Test1 table
I want to a get records with this condition,
if(Test1.id=Test1Update.id){
select macthing values from Test1Update tables,and remaining unmatched values from Test1 table
}
else if(Test1.id!=Test1Update.id)
{
select * from Test1 only
}
As a example
Test1 has data like
Test1
id name address
1 john Ca
2 mary La
and Test1Update has data like
id name address
1 john Las Vega
s
Now i want to get only matching records from Test1Update table and all unmatching records from
Test1 table so final output will be
id name address
1 john Las Vegas
2 mary La
It means that old entry should be replced whenever there is any match
found with Test1Update.
How can I do that using a select query or by procedure?
Show me some way .
Assuming that all the id in Test1Update are present in Test1 that can be done simply with:
See it work here: http://sqlfiddle.com/#!3/4a1f2/3/0
With the LEFT JOIN, if there is no matching entry in TEST1UPDATE, TEST1UPDATE.NAME and TEST1UPDATE.ADDRESS are null.
COALESCE gives you the first non null argument, so you’ve got the values from TEST1UPDATE if present, else from TEST1.
Edit for the UNION solution:
If there are a lot of fields, the COALESCE solution might be a bit verbose and you would like to use a UNION: