我在asp.net中使用sql server mambership進行網站創作。我有一個表,所有products.here我有用戶與一些產品是現有的所有產品。現在我想顯示其餘的產品不存在與用戶從所有products.plz幫助我。我使用網格來表示產品。 我需要一個查詢,以符合我的條件,謝謝查詢表中的其他項目。一些已經存在於另一個
0
A
回答
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。
相關問題
- 1. 檢查一個項目是否已經存在於JComboBox中?
- 2. 檢查一個項目是否已經存在於listbox1中
- 3. 查找列表中的項目存在於另一個列表
- 4. 檢查一個項目是否已經存在於一個txt文件中
- 5. 我如何檢查一個項目是否已經存在於列表中?
- 6. 檢查列表框中的某個項目是否已經存在於許多其他列表框中?
- 7. 檢查一個項目是否已經在另一個網格中
- 8. SQL查詢 - 不在一組已經使用的項目中
- 9. SQL查詢 - 一個表中的項目列表不在另一個表中
- 10. MySQL查詢其中一對值不存在於另一個表中
- 11. 查找一個項目是否已經存在於STL隊列中
- 12. 創建SQL查詢插入一個值,如果另一個已經存在
- 13. 從一個表中返回不存在於另一個表中的SQL查詢
- 14. 如何刪除已存在於其他ListBox項目中的一個ListBox中的項目
- 15. 算法在同一列表中的其他項目,其中一些有限制
- 16. 從一個表中選擇用戶如果已經在其他
- 17. Laravel查詢其中的值等於另一個表中的值
- 18. 如何檢查項目[name&subItem的文本]是否已經存在於另一個listView中?
- 19. 如何指定列中的值必須已經存在於另一個表中?
- 20. 每個項目清單,其他項目在另一個列表進行比較
- 21. JPA,堅持OneToMany的實體,其中一些已經存在
- 22. 取其中COUNT()在另一個表和ID犯規已經存在
- 23. 查詢表中與ORMLITE其他表中其他項無關的所有項目
- 24. 在列表中已經添加一個項目的每個項目之間
- 25. 追加如果該項目不存在於另一個表中?
- 26. 檢索其中一個項目列表中存在項目的項目列表
- 27. 如果存在於另一個查詢(表)中,則替換值?
- 28. 從一個表中查詢基於另一個表的數據
- 29. 檢查div內容是否已經存在於另一個div中?
- 30. 插入數據從一個表到另一個從一個表到另一個離開已經存在的行
真的,真的很短的詳細信息.... –