我有以下表格
产品 -- 为产品 ID 存储 ProductRelation -- 存储链接的产品 ID
DECLARE @Product table(ProductID int)
DECLARE @ProductRelation TABLE (FirstProductID int,SecondProductID int)
INSERT INTO @Product
SELECT 1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
UNION ALL
SELECT 7
UNION ALL
SELECT 8
UNION ALL
SELECT 9
UNION ALL
SELECT 10
--SELECT * FROM @Product
INSERT INTO @ProductRelation
SELECT 1,2
UNION ALL
SELECT 3,5
UNION ALL
SELECT 2,6
UNION ALL
SELECT 1,4
UNION ALL
SELECT 1,4
--SELECT * FROM @ProductRelation
SELECT ProductID,'Not Linked' AS 'Relation' FROM @Product
UNION
SELECT FirstProductID,'Linked' from @ProductRelation
UNION
SELECT SecondProductID ,'Linked' FROM @ProductRelation
以上查询结果重复ProductID
我想选择不同的 ProductID ...如果产品 ID 之间存在关系,那么它应该显示带有“已链接”的 ProductID 如果没有关系,则带有“未链接”的 ProductID
我想要这样的预期结果
ProductID Relation
1 Linked
2 Linked
3 Linked
4 Linked
5 Linked
6 Linked
7 Not Linked
8 Not Linked
9 Not Linked
10 Not Linked