sql server - How to get data from multiple tables -
table1 <table> <tr> <td>id</td> <td>name</td> <td>rate</td> </tr> <tr> <td>1</td> <td>tomato</td> <td>10</td> </tr> <tr> <td>2</td> <td>brinjal</td> <td>10</td> </tr> <tr> <td>3</td> <td>potato</td> <td>30</td> </tr> </table></br></br> table2 <table> <tr> <td>id</td> <td>new_rates</td> </tr> <tr> <td>1</td> <td>45</td> </tr> <tr> <td>2</td> <td>30</td> </tr> </table> </br></br> want result <table> <tr> <td>id</td> <td>name</td> <td>rate</td> </tr> <tr> <td>1</td> <td>tomato</td> <td>45</td> </tr> <tr> <td>2</td> <td>brinjal</td> <td>30</td> </tr> <tr> <td>3</td> <td>potato</td> <td>30</td> </tr> </table>
table 1
id name rate 1 tomato 10 2 brinjal 10 3 cucumber 30
table 2
id new_rate 1 25
i want result
id name rate 1 tomato 25 2 brinjal 10 3 cucumber 30
select table1.id, table1.name, isnull(table2.new_rate, table1.rate) rate table1 left join table2 on table1.id = table2.id;
you need left outer join
on second table because second table doesn't have matching rows rows in first table.
then can use isnull()
function show value table 2, or if 1 doesn't exist show table 1.
Comments
Post a Comment