在嵌套的SELECT语句中使用SQL结果变量

时间:2012-08-01 19:41:52

标签: sql select insert nested

我真的不确定这个标题是否是我想说的,但它是我能来的最接近的。我想要做的是从查询中获取初始结果,并在INSERT语句中的嵌套Select语句中使用它们。这是我的例子: 这是我的初步查询,它给了我正在寻找的工作正确;

    SELECT Min(Inquirer.ID) as MinID, Max(Inquirer.ID) as MaxID, 
    Min(Inquirer.Program) as MinProg, Max(Inquirer.Program) as MaxProg,
     COUNT(Inquirer.Name) AS NumOccurrences
    FROM Inquirer
    GROUP BY Inquirer.Name
    HAVING ( COUNT(*) = 2 )

结果显示如下;

MinID         MaxID           MinProg          MaxProg          NumOccurrences
 27             81            Pavilion         Appleton              2

我想使用我在相应的INSERT中检索的MinID和MaxID值,嵌套的SELECT是这样的;

Insert into Transfer(InquiryID_fk, ReferElsewhere, ReferMcLean, ReferExternal, ReferredFrom, TransferInquiryID_fk)
Values(SELECT Inquiry.ID from Inquiry 
Where Inquiry.InquirerID_fk = MinID,'Yes',MaxProg,NULL,MinProg,
SELECT Inquiry.ID from Inquiry WHERE Inquiry.InquirerID_fk = MaxID)

这就是它的确如此,不确定它是否可以完成或如何实现它,我是否必须事先制作临时表并循环获取值? 如有任何帮助,请提前感谢,

NickG

4 个答案:

答案 0 :(得分:2)

在这种情况下不要使用VALUES,请直接使用INSERT INTO()... SELECT。

E.g。

INSERT INTO Transfer(InquiryID_fk, ReferElsewhere, ReferMcLean, ReferExternal, ReferredFrom, TransferInquiryID_fk)
SELECT Min(Inquirer.ID), 'Yes', Max(Inquirer.Program), NULL,     Min(Inquirer.Program), Max(Inquirer.ID)
    FROM Inquirer
    GROUP BY Inquirer.Name
    HAVING ( COUNT(*) = 2 )

答案 1 :(得分:1)

尝试使用此代码

Insert into Transfer(
InquiryID_fk, 
ReferElsewhere,
 ReferMcLean,
 ReferExternal, 
ReferredFrom, 
TransferInquiryID_fk)
SELECT Min(Inquirer.id), `yes`,Max(Inquirer.Program), null, Min(Inquirer.Program), Max(Inquirer.ID)
FROM Inquirer
GROUP BY Inquirer.Name
HAVING ( COUNT(*) = 2 )

答案 2 :(得分:0)

使用新表解决您的问题,但我不建议您为查询添加太多嵌套选择,这可能会降低性能。

在你的情况下,我会为这个

创建一个存储过程
  • 使用两个变量为此创建存储过程:MinIdMaxId
  • 在您的第一个选择(您获得minid和maxId)中设置这些变量的值

他们在您的查询中使用以下变量:

Insert into Transfer(InquiryID_fk, ReferElsewhere, ReferMcLean, ReferExternal, ReferredFrom, TransferInquiryID_fk)
 SELECT Inquiry.ID from Inquiry Where Inquiry.InquirerID_fk = @MinID
 ,'Yes'
 ,MaxProg
 ,NULL
 ,MinProg
 ,SELECT Inquiry.ID from Inquiry WHERE Inquiry.InquirerID_fk = @MaxID

答案 3 :(得分:0)

这应该有用。

;with cte as
(
    SELECT Min(Inquirer.ID) as MinID, Max(Inquirer.ID) as MaxID, 
    Min(Inquirer.Program) as MinProg, Max(Inquirer.Program) as MaxProg,
    COUNT(Inquirer.Name) AS NumOccurrences
    FROM Inquirer
    GROUP BY Inquirer.Name
    HAVING ( COUNT(*) = 2 )
)
    Insert into Transfer
    (InquiryID_fk, ReferElsewhere, ReferMcLean, ReferExternal, ReferredFrom, TransferInquiryID_fk)
    SELECT imin.id,'Yes',MaxProg,NULL,MinProg,imax.ID 
    from cte
        inner join inquiry imin on MinID = imin.inquirer_Id 
        inner join inquiry imax on MaxID = imax.inquirer_Id 

但我认为你想要的是选择阅读......

    SELECT imin.id,'Yes',qmax.Program,NULL,qmin.Program,imax.ID 
    from cte
        inner join inquiry imin on MinID = imin.inquirer_Id 
                 inner join inquirer qmin on minid = qmin.id
        inner join inquiry imax on MaxID = imax.inquirer_Id 
                 inner join inquirer qmax on minid = qmax.id

...但这取决于您拥有哪种SQL