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
 General SQL Server Forums
 New to SQL Server Programming
 Guidance on using IN

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-05-15 : 13:40:52
I have two tables:

Offense_Conversion
OffensePropertyConversion

I want to do a select from Offense_Conversion to show me where the value in columns Offensno exists in Offensepropertyconversion.

SELECT * FROM OffensePropertyConversion WHERE offenseno IN offense_conversion

is a no go.

any guidance is appreciated.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-15 : 13:45:03
[code]SELECT * FROM OffensePropertyConversion
WHERE offenseno IN (SELECT offenseno FROM offense_conversion)

-- Better:

SELECT A.* FROM OffensePropertyConversion A
INNER JOIN offense_conversion B ON A.offenseno=B.offenseno

-- Maybe best:

SELECT * FROM OffensePropertyConversion A
WHERE EXISTS(SELECT * FROM offense_conversion WHERE offenseno=A.offenseno)[/code]
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-05-15 : 13:58:58
thanks rob. i like the last one. looks most familiar to me.

i hates da joins.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-15 : 14:02:11
quote:
i hates da joins
Well, relational databases aren't the data store for you. They're kinda sorta optimized for joining.
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-05-15 : 14:19:36
i know. i have to get past that resistance on my part.
Go to Top of Page
   

- Advertisement -