Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Retrieving data from 2 tables

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-01-04 : 08:26:28
sonal82 writes "I hv 2 tables table1 and table2 which have 2 common fields one is "tid" and second is "dayno". I want records from at least one of the tables i.e.

table1:
tid dayno
1 1
2 2
3 3
4 4
5 5

table2:
tid1 tid dayno city
1 1 6 a
2 1 1 b
3 2 7 c
4 5 2 d
5 5 1 e

I want records as:
tid dayno city
1 1 b
2 2 Null
3 3 Null
4 4 Null
5 5 Null

tid 3 and 4, are not in table2, but i want to display it, 2 and 5 are present but dayno is not matched hence city will be Null.

plz help me
thanks in advance"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-04 : 08:30:51
Use Left join

Select T1.tid, T1.dayno, T2.city from Table1 T1 left join Table2 T2 on T1.tid=T2.tid1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-04 : 08:35:51
[code]select t1.tid, t1.dayno, t2.city
from table1 t1 left join table2 t2
on t1.tid = t2.tid
and t1.dayno = t2.dayno[/code]



-----------------
[KH]

2006 a new beginning
Go to Top of Page
   

- Advertisement -