![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
I'm having trouble figuring out the proper SQL statement to use for this. Any help would be greatly appreciated: Three tables are involved. Example data: Table: Products lngID strProduct 1 Toy 2 Book 3 Fruit 4 CD 5 Game Table: Groups lngID strGroup 1 Sales 2 Marketing 3 Executive Table: GroupProducts lngID lngProductID lngGroupID 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 2 2 7 4 2 I'm going to let people add new products to any particular group, but obviously I don't want to show them products that are already assigned to their group, so... What I need is a query that will return the ID and name of any product that's NOT already assigned to a particular group. For this example, let's use group 2. So the query should return products 1, 3 and 5. I've tried various LEFT and RIGHT joins but obviously I'm just not getting something because I keep getting back basically the entire contents of the Products table, even though some of the items have already been assigned to the group in question. Thanks in advance... LZ |
#2
| |||
| |||
|
|
It all makes sense to me except the 'SELECT 1' in the second part of the query...what does that provide? Does it make sure the query doesn't return duplicates? |
|
Hi again, Actually, I think I figured it out...I got rid of the first two lines (since I already have the variable set up in my ASP code) and added an AND between the last two lines. So the final sql statement looks like this (modified a bit with actual field names): sql = "SELECT ProductID, ProductName FROM Products sqlA WHERE NOT EXISTS (SELECT 1 FROM GroupProducts sqlB WHERE sqlB.lngProductID = sqlA.ProductID AND sqlB.lngGroupID = " & lngGroupID & ");" It all makes sense to me except the 'SELECT 1' in the second part of the query...what does that provide? Does it make sure the query doesn't return duplicates? Thanks again for your help...VERY much appreciated!! LZ -----Original Message----- Try: declare @v_lnggroupid int select @v_lnggroupid = 2 select lngID, strProduct from products a where not exists (select 1 from groupproducts b where b.lngproductid = a.lngID b.lnggroupid = @v_lnggroupid) -- -Vishal Lou Zucaro <lou (AT) netspecialists (DOT) com> wrote in message news:038801c3540c$874c2140$a101280a (AT) phx (DOT) gbl... I'm having trouble figuring out the proper SQL statement to use for this. Any help would be greatly appreciated: Three tables are involved. Example data: Table: Products lngID strProduct 1 Toy 2 Book 3 Fruit 4 CD 5 Game Table: Groups lngID strGroup 1 Sales 2 Marketing 3 Executive Table: GroupProducts lngID lngProductID lngGroupID 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 2 2 7 4 2 I'm going to let people add new products to any particular group, but obviously I don't want to show them products that are already assigned to their group, so... What I need is a query that will return the ID and name of any product that's NOT already assigned to a particular group. For this example, let's use group 2. So the query should return products 1, 3 and 5. I've tried various LEFT and RIGHT joins but obviously I'm just not getting something because I keep getting back basically the entire contents of the Products table, even though some of the items have already been assigned to the group in question. Thanks in advance... LZ . |
![]() |
| Thread Tools | |
| Display Modes | |
| |