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 2005 Forums
 Transact-SQL (2005)
 match on two columns

Author  Topic 

jbpblog
Starting Member

4 Posts

Posted - 2010-01-11 : 00:22:20
Table1 has four columns: ID(int),Name(nvarchar), RefCode(char),RefCodeValue(nvarchar)
Table2 has two columns; RefCode(char), RefCodeValue(nvarchar)

example:
refCode | RefCodeValue
AT | F.o.D.
AT | Dc.o.D.
CP | F.o.D.
CP | ghhm

I would like to return those records of table1 where the RefCodeValue and RefCode match in both tables.

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-01-11 : 00:24:54
put a join,

Select id,name,t1.RefCode,t2.RefCodeValue from table1 t1 inner join
table2 t1 on t1.RefCode=t2.RefCode and t1.RefCodeValue=t2.RefCodeValue

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-01-11 : 00:25:51
[code]
select *
from table1 inner join table2 on table1.refcode = table2.ref_code
and table1.refcodevalue = table2.ref_codevalue
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jbpblog
Starting Member

4 Posts

Posted - 2010-01-11 : 00:32:08
Thank you - and how would I reverse the query to get only the records from t1 that DON'T have a match on t2?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-01-11 : 00:34:18
[code]
select *
from table1 LEFT join table2 on table1.refcode = table2.ref_code
and table1.refcodevalue = table2.ref_codevalue
where t2.refcode is null
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-01-11 : 00:34:36
or use NOT EXISTS()


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jbpblog
Starting Member

4 Posts

Posted - 2010-01-17 : 10:48:55
Thank you!
Go to Top of Page
   

- Advertisement -