C# 查询表中的其余项。某些项已存在于另一个表中
我正在使用asp.net中的sql server mambership创建网站。我有一个包含所有产品的表。在这里,我有一个包含所有产品中存在的某些产品的用户。现在我想显示所有产品中用户不存在的其他产品。请帮助我。iam使用网格表示产品。C# 查询表中的其余项。某些项已存在于另一个表中,c#,asp.net,sql-server-2008,C#,Asp.net,Sql Server 2008,我正在使用asp.net中的sql server mambership创建网站。我有一个包含所有产品的表。在这里,我有一个包含所有产品中存在的某些产品的用户。现在我想显示所有产品中用户不存在的其他产品。请帮助我。iam使用网格表示产品。 我需要一个符合我条件的查询谢谢你的问题不是很清楚,你有产品ID列表吗?表格的格式是怎样的 您可能需要以下内容: SELECT * FROM products_all WHERE id NOT IN (1, 2, 3, 4, 5) 在这种情况下,1、2、3、4、
我需要一个符合我条件的查询谢谢你的问题不是很清楚,你有产品ID列表吗?表格的格式是怎样的 您可能需要以下内容:
SELECT *
FROM products_all
WHERE id NOT IN (1, 2, 3, 4, 5)
在这种情况下,1、2、3、4、5是用户拥有的ID。您可以对
UserProducts
表使用left-outer-join
,并检查ProductID是否为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
真的,真的缺少详细的信息。。。。