Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I have data like this

Table 1 has following data
SQL
sno name 
1    a
2    b 
3    c
4    d
5    e


Table 2 has following data
SQL
sno name
1   b
2   a
3   c
4   d
6   f

now how can i write a query that if name in both the tables are equal the it has to get sno of table2 else get sno of table1 and along with this the join query should return all the rows in both Table 1 and Table2
Posted
Updated 30-Nov-14 22:52pm
v2

Check this
SQL Joins - W3Schools[^]

Also this may help you

SQL
select isnull(b.sno,a,sno),a.*,b.* 
from table1 a Full Outer Join table2 on a.name=b.name
 
Share this answer
 
This query should return all records with equal names:
SQL
SELECT COALESCE(t2.sno, t1.sno) AS NewSno, t2.name
FROM table1 AS t1 INNER JOIN table2 as t2 ON t1.name = t2.name


For further information about different types of joins, please read this: Visual Representation of SQL Joins[^]
COALESCE[^]
 
Share this answer
 
see this..
SQL
select
(case when a.name=b.name then isnull(b.sno,a.sno) else isnull(a.sno,b.sno) end) as sno ,
isnull(a.name,b.name) from [dbo].[Table_1] a

full join

[dbo].[Table_2] b on a.sno=b.sno



Here http://stackoverflow.com/questions/18828641/sql-difference-between-coalesce-and-isnull[^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900