查询将类似于:
SELECT T2.HeadId , T2.SubId 从 Temp2 T2 INNER JOIN Temp1 T1H ON T1H.Id = T2.HeadId INNER JOIN Temp1 T1S ON T1S.Id = T2.SubId WHERE T1S.IsMarket = 1 AND T1H.IsMarket = 1
请使用具有表创建、填充然后输出的示例运行以下查询:
创建表 Temp1 ( Id INT IDENTITY(1, 1) , IsMarket BIT )
创建表 Temp2 ( HeadId INT, SubId INT )
插入 Temp1 (IsMarket) 值 (0) 插入 Temp1 (IsMarket) 值 (0) 插入 Temp1 (IsMarket) 值 (0) 插入 Temp1 (IsMarket) 值 (0) 插入 Temp1 (IsMarket) 值 (0) INSERT INTO Temp1 (IsMarket) 值 (0) INSERT INTO Temp1 (IsMarket) 值 (0) INSERT INTO Temp1 (IsMarket) 值 (0) INSERT INTO Temp1 (IsMarket) 值 (0) - 运行 9 次 INSERT INTO Temp1 (IsMarket) ) 值 (1) 插入 Temp1 (IsMarket) 值 (1) 插入 Temp1 (IsMarket) 值 (1) 插入 Temp1 (IsMarket) 值 (1) 插入 Temp1 (IsMarket) 值 (1) 插入 Temp1 (IsMarket) ) 值 (1) 插入温度 1 (IsMarket ) 值 (1) 插入 Temp1 (IsMarket) 值 (1) 插入 Temp1 (IsMarket) 值 (1)
插入 Temp2 ( HeadId, SubId ) 值 (1, 2 ) 插入 Temp2 ( HeadId, SubId ) 值 (1, 10 ) 插入 Temp2 ( HeadId, SubId ) 值 (15, 2 ) 插入 Temp2 ( HeadId, SubId )值 ( 10, 1 )
插入到 Temp2 ( HeadId, SubId ) 值 ( 10, 15 )
SELECT T2.HeadId , T2.SubId 从 Temp2 T2 INNER JOIN Temp1 T1H ON T1H.Id = T2.HeadId INNER JOIN Temp1 T1S ON T1S.Id = T2.SubId WHERE T1S.IsMarket = 1 AND T1H.IsMarket = 1