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)
 Inner Join on more than one column?

Author  Topic 

fizzer
Starting Member

10 Posts

Posted - 2006-09-26 : 05:38:14
Hi,

Can you have an inner join on more than one column. I have two tables with composite primary keys. I can't create a join on a single column so how would I create the join on two columns?

Hope that makes sense

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-26 : 05:59:28
SELECT *
FROM FirstTable ft
INNER JOIN SecondTable st ON st.ColA = ft.Col1 AND st.ColB = ft.Col2


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

fizzer
Starting Member

10 Posts

Posted - 2006-09-26 : 06:12:57
Many thanks. That did the trick.
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-26 : 08:14:07
aha how about this, if both the fields are varchar


SELECT *
FROM FirstTable ft
INNER JOIN SecondTable st ON st.ColA + '-'+ st.ColB = ft.Col1+'-'+ft.Col2


Chirag
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-26 : 09:24:02
Learn SQL

http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

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

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2006-09-27 : 00:18:23
quote:
Originally posted by chiragkhabaria

aha how about this, if both the fields are varchar


SELECT *
FROM FirstTable ft
INNER JOIN SecondTable st ON st.ColA + '-'+ st.ColB = ft.Col1+'-'+ft.Col2


Chirag


Only if you never want to use an index.
Go to Top of Page
   

- Advertisement -