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
 SQL Server Development (2000)
 help on joining tables

Author  Topic 

abyss
Starting Member

1 Post

Posted - 2004-12-16 : 03:29:06
need help on biuding a query...
i.e. there are 2 tables:
================== ===========================
ID name memo ID mail url
================== ===========================
5 me nomemo 92 trash@mail.com www.t.co
20 you 1k no mail www.12.lt
92 who text
101 any memo
1k many

how should the query look to obtain rezult containing all table1 fields + all table2 fields, even if ID2<>ID1 (the field must be blanc)

expecting 4 result.... :
================================================
ID name memo mail url
=================================================
5 me nomemo
20 you
92 who text trash@mail.com www.t.co
101 any memo no mail www.12.lt
1k many

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2004-12-16 : 13:22:40
If I understand the requirements correctly, you want:

1) Every row of table1
2) If there is a matching row in table2, use that data also
3) If there is NOT a matching row in table2, use blanks for table2's columns

select t1.ID, t1.Name, t1.Memo, coalesce(t2.Mail, ''), coalesce(t2.url, '')
from table1 t1
LEFT OUTER JOIN table2 t2
on t1.ID = t2.ID

You can look up JOIN in books online (BOL) for a discussion of what the INNER, OUTER and CROSS JOINS are and how to use them.

HTH

=================================================================

Happy Holidays!
Go to Top of Page
   

- Advertisement -