在条件如何的第二查询中如何使用第一查询的结果?

时间:2019-04-04 07:50:31

标签: sql sql-server

我必须选择两个表数据,一个是PackageData,另一个是PackageDataDetails。这是PackageData是父表,而PackageDataDetails是子表。

请参见以下代码:

SELECT 
    PD.Id, PD.MemberId, PD.Merchant, PD.Weight, PD.Remarks
FROM 
    [dbo].[PackageData] PD
INNER JOIN 
    Account A ON A.MemberId = PD.MemberId
INNER JOIN 
    Users U ON U.Id = A.UserId AND (@UserId IS NULL OR U.Id = @UserId)
WHERE 
    (@Status IS NULL OR @Status = '' OR  PD.SystemStatus = @Status)
    AND (@Id IS NULL OR PD.Id = @Id)
ORDER BY 
    MD.Id
    OFFSET @PageSize * (@PageNo+1 - 1) ROWS  
    FETCH NEXT @PageSize ROWS ONLY 
OPTION (RECOMPILE);

SELECT 
    PDD.Id, PDD.PackageDataId, PDD.Description, PDD.Quantity, PDD.Status
FROM 
    [dbo].[PackageDataDetails] PDD
INNER JOIN 
    [dbo].[PackageData] PD ON PD.Id = PDD.PackageDataId
WHERE 
    PDD.PackageDataId IN ({PD.Id of Above Select query})

此代码块以存储过程编写。在这里,我不想在第二个查询条件所在的地方重写第一个查询。

我必须在一个API响应中使用此存储过程,才能仅在一个DB调用中执行操作。请分享您的想法以解决此问题。

4 个答案:

答案 0 :(得分:1)

您可以在表变量中插入第一个查询数据,然后在第二个查询中从表变量中获取ID。如下所示。

代码:

 Declare @tblData TABLE
    ( 
        Id          Int,
        MemberId                    Int,
        Merchant    NVARCHAR(Max),
        Weight  NVARCHAR(Max),
        Remarks               Nvarchar(Max)

    );

   Insert Into @tblData
(
    Id,
    MemberId,
    Merchant,
    Weight,
    Remarks
)

SELECT PD.Id ,PD.MemberId, PD.Merchant,PD.Weight,PD.Remarks
FROM [dbo].[PackageData] PD
INNER JOIN Account A ON A.MemberId = PD.MemberId
INNER JOIN Users U ON U.Id = A.UserId AND (@UserId IS NULL OR U.Id = @UserId)
WHERE (@Status IS NULL OR @Status = '' OR  PD.SystemStatus = @Status)
AND (@Id IS NULL OR PD.Id = @Id)
ORDER BY MD.Id
OFFSET @PageSize * (@PageNo+1 - 1) ROWS  FETCH NEXT @PageSize ROWS ONLY OPTION (RECOMPILE);

SELECT PDD.Id, PDD.PackageDataId, PDD.Description, PDD.Quantity, PDD.Status
FROM [dbo].[PackageDataDetails] PDD
INNER JOIN [dbo].[PackageData] PD ON PD.Id = PDD.PackageDataId
WHERE PDD.PackageDataId IN (select Id from @tblData )

答案 1 :(得分:0)

请尝试以下查询。

首次查询

SELECT 
    PD.Id ,PD.MemberId, PD.Merchant,PD.Weight,PD.Remarks
    INTO #temp 
    FROM [dbo].[PackageData] PD
    INNER JOIN Account A ON A.MemberId = PD.MemberId
    INNER JOIN Users U ON U.Id = A.UserId AND (@UserId IS NULL OR U.Id = @UserId)
    WHERE (@Status IS NULL OR @Status = '' OR  PD.SystemStatus = @Status)
    AND (@Id IS NULL OR PD.Id = @Id)
    ORDER BY MD.Id
    OFFSET @PageSize * (@PageNo+1 - 1) ROWS  FETCH NEXT @PageSize ROWS ONLY OPTION (RECOMPILE);

获取输出优先查询

 SELECT * FROM #temp 

第二个查询,带第一个查询结果。

SELECT 
     PDD.Id, PDD.PackageDataId, PDD.Description, PDD.Quantity, PDD.Status
FROM [dbo].[PackageDataDetails] PDD
    INNER JOIN [dbo].[PackageData] PD ON PD.Id = PDD.PackageDataId
    WHERE PDD.PackageDataId IN (SELECT PD.Id FROM #temp)

DROP TABLE #temp

答案 2 :(得分:0)

使用CTE(公用表表达式):

即:

    ;with CTE1 as (
 select 1 as Col1,
 2 as Col2,
 3 as Col3
), CTE2 as (
select 1 as Col1,
        5 as Col2,
        6 as Col3
)select * from CTE1 where CTE1.Col1 IN (select Col1 from CTE2)

答案 3 :(得分:0)

如果只需要第一个查询来提供第二个查询,则将第一个查询包装在CTE中,并在运行第二个查询时参考:

;WITH PackageDataFiltered AS
(
    SELECT PD.Id ,PD.MemberId, PD.Merchant,PD.Weight,PD.Remarks
    FROM [dbo].[PackageData] PD
    INNER JOIN Account A ON A.MemberId = PD.MemberId
    INNER JOIN Users U ON U.Id = A.UserId AND (@UserId IS NULL OR U.Id = @UserId)
    WHERE (@Status IS NULL OR @Status = '' OR  PD.SystemStatus = @Status)
    AND (@Id IS NULL OR PD.Id = @Id)
    ORDER BY MD.Id
    OFFSET @PageSize * (@PageNo+1 - 1) ROWS  FETCH NEXT @PageSize ROWS ONLY 
)
SELECT PDD.Id, PDD.PackageDataId, PDD.Description, PDD.Quantity, PDD.Status
-- You can refer to fields from the CTE here too
-- like select the PDF.Merchant or PDF.Remarks
FROM [dbo].[PackageDataDetails] PDD
INNER JOIN [dbo].[PackageData] PD ON PD.Id = PDD.PackageDataId
INNER JOIN PackageDataFiltered PDF ON PDF.Id = PDD.PackageDataId