Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询表中的其余项。某些项已存在于另一个表中_C#_Asp.net_Sql Server 2008 - Fatal编程技术网

C# 查询表中的其余项。某些项已存在于另一个表中

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、

我正在使用asp.net中的sql server mambership创建网站。我有一个包含所有产品的表。在这里,我有一个包含所有产品中存在的某些产品的用户。现在我想显示所有产品中用户不存在的其他产品。请帮助我。iam使用网格表示产品。
我需要一个符合我条件的查询谢谢你的问题不是很清楚,你有产品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

真的,真的缺少详细的信息。。。。