Sql子查询无法正常工作

时间:2014-06-29 12:03:18

标签: mysql sql sql-server

这是我的子查询,但它不起作用:

SELECT     CommitmentLog.app_id, CommitmentLog.CommitAmt, CommitmentLog.CommitDate, 
    CommitmentLog.status, CommitmentLog.ConfirmDate, Appreg.uid, Appreg.SpillBy,  
    Appreg.spill, Appreg.Email, Appreg.pass, Appreg.intro_id, Appreg.app_id AS Expr1
FROM  CommitmentLog 
INNER JOIN  Appreg ON CommitmentLog.app_id = Appreg.app_id 
where Appreg.app_id = 
(
    SELECT dbo.UIDFromApp_ID(Intro_id) as 'SponsorUserID', [app_id],[uid], [app_name], [Side], [doj] 
    FROM [v_Appreg] 
    WHERE ([Intro_id] = 1496)
)

它给出错误:

Msg 116, Level 16, State 1, Line 4
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

3 个答案:

答案 0 :(得分:0)

假设您正在使用MSSQL(错误看起来像SQL-Serverish)并且您在子查询中寻找app_id,那么这样的事情应该是您所追求的。

SELECT 
    CommitmentLog.app_id, 
    CommitmentLog.CommitAmt, 
    CommitmentLog.CommitDate, 
    CommitmentLog.status, 
    CommitmentLog.ConfirmDate, 
    Appreg.uid, 
    Appreg.SpillBy, 
    Appreg.spill, 
    Appreg.Email, 
    Appreg.pass, 
    Appreg.intro_id, 
    Appreg.app_id AS Expr1

FROM CommitmentLog 
INNER JOIN Appreg 
    ON CommitmentLog.app_id = Appreg.app_id 
WHERE Appreg.app_id IN 
    (SELECT [app_id] FROM [v_Appreg] WHERE ([Intro_id] = 1496))

正如错误所说,在尝试使用IN子句时,子查询中只能有一个值。

答案 1 :(得分:0)

由于错误消息告诉您只需要在子查询中检索一个值。

SELECT
    CommitmentLog.app_id,
    CommitmentLog.CommitAmt,
    CommitmentLog.CommitDate, 
    CommitmentLog.status,
    CommitmentLog.ConfirmDate,
    Appreg.uid,
    Appreg.SpillBy,  
    Appreg.spill,
    Appreg.Email,
    Appreg.pass,
    Appreg.intro_id,
    Appreg.app_id AS Expr1

FROM
    CommitmentLog INNER JOIN  Appreg ON CommitmentLog.app_id = Appreg.app_id

WHERE
    Appreg.app_id = 
        (SELECT dbo.UIDFromApp_ID(Intro_id) as 'SponsorUserID' FROM [v_Appreg] WHERE [Intro_id] = 1496)

答案 2 :(得分:0)

错误消息告诉您确切的错误。你想说

where appreg.app_id = 
(select dbo.UIDFromApp_ID(Intro_id) as 'SponsorUserID', [app_id], etc
那是暧昧的。您只需为该字段分配一个值。

一旦你解决了这个问题,你就会遇到另一个问题。如果您的子查询返回多行,则它再次模糊不清。你必须为此做些什么。细节取决于您的要求。