Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Microsoft SQL Server > Re: Selecting s...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 5 Topic 11024 of 11517
Post > Topic >>

Re: Selecting several types

by Erland Sommarskog <esquel@[EMAIL PROTECTED] > May 7, 2008 at 09:46 PM

Danny (adler.danny@[EMAIL PROTECTED]
) writes:
> So I have these tables of items and item-types, and the items can be
> of several types:
> 
> Items:
> ID ; Name
> 1 ; Item1
> 2 ; Item2
> 
> ItemType:
> ID ; Type
> 100 ; Type1
> 200 ; Type2
> 300 ; Type3
> 
> Now, to link these 2 I have a "linking" table:
> 
> LinkingTable:
> ItemID ; ItemTypeID
> 1 ; 100
> 1 ; 200
> 2 ; 100
> 2 ; 300
> 
> Now I want to select all the items of type 100 or 200...
> 
> How can I do that without getting a duplicate row of item 1??
 
Another solution is to use EXISTS:

   SELECT i.item_id, i.item_name
   FROM   items i
   WHERE  EXISTS (SELECT *
                  FROM   linkingtable l
                  WHERE  i.item_id = l.item_id
                    AND  l.item_type_id IN (100, 200))



-- 
Erland Sommarskog, SQL Server MVP, esquel@[EMAIL PROTECTED]
 Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 




 5 Posts in Topic:
Selecting several types
Danny <adler.danny@[EM  2008-05-07 06:00:30 
Re: Selecting several types
"Plamen Ratchev"  2008-05-07 10:17:20 
Re: Selecting several types
--CELKO-- <jcelko212@[  2008-05-07 13:05:28 
Re: Selecting several types
Ed Murphy <emurphy42@[  2008-05-07 20:32:57 
Re: Selecting several types
Erland Sommarskog <esq  2008-05-07 21:46:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 1:07:02 CST 2008.