2011-07-12 93 views
0

我在asp.net中使用sql server mambership進行網站創作。我有一個表,所有products.here我有用戶與一些產品是現有的所有產品。現在我想顯示其餘的產品不存在與用戶從所有products.plz幫助我。我使用網格來表示產品。 我需要一個查詢,以符合我的條件,謝謝查詢表中的其他項目。一些已經存在於另一個

+3

真的,真的很短的詳細信息.... –

回答

0

您可以使用left outer join對您的UserProducts表進行檢查,並檢查ProductID is null

帶有表變量而不是真表的一些虛擬數據的示例。

-- Products table with all products 
declare @Products table (ProductID int, ProductName varchar(10)) 

-- Products connected to a user 
declare @UserProducts table (UserID int, ProductID int) 

-- Add all products to products table 
insert into @Products(ProductID, ProductName) values 
(1, 'Prod 1'), 
(2, 'Prod 2'), 
(3, 'Prod 3'), 
(4, 'Prod 4'), 
(5, 'Prod 5') 

-- Add products already connected to user 
insert into @UserProducts(UserID, ProductID) values 
(1, 1), 
(1, 2), 
(2, 1), 
(2, 3) 

-- Fetch all products for UserID = 1 
declare @UserID int = 1 

select A.ProductID, 
     A.ProductName 
from @Products as A 
    left outer join @UserProducts U 
    on A.ProductID = U.ProductID and 
     U.UserID = @UserID 
where U.ProductID is null 

結果:

ProductID ProductName 
----------- ----------- 
3   Prod 3 
4   Prod 4 
5   Prod 5 
0

您的問題不是很清楚,您是否有產品ID列表?表格是如何格式化的?

你可能想是這樣的:

SELECT * 
FROM products_all 
WHERE id NOT IN (1, 2, 3, 4, 5) 

在這種情況下,1,2,3,4,5,你的用戶的ID。

相關問題