子查询在内部联接上返回了多个值错误

时间:2013-09-25 22:22:31

标签: sql-server

获取旧的

  

Msg 512,Level 16,State 1,Line 1 Subquery返回的值超过1   值。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。“

我尝试运行此脚本时出现

错误:

SELECT  name_first,
        name_last
FROM person
INNER JOIN message
    ON person.person_id = (
        SELECT message.recipient_id 
        FROM message 
        WHERE message.filename = '1003-5ivGbUqIz80r0NwCl9kzWpDjYDit9L.mp3')

任何帮助弄清楚我做错了什么都会受到赞赏。

2 个答案:

答案 0 :(得分:3)

您不需要有子查询。

SELECT DISTINCT name_first, name_last
FROM   person 
       INNER JOIN message 
         ON person.person_id = message.recipient_id  
WHERE  message.filename = '1003-5ivGbUqIz80r0NwCl9kzWpDjYDit9L.mp3'
DISTINCT子句中指定了

SELECT,只显示其唯一的人名,假设它可以包含多条消息。

答案 1 :(得分:0)

you can use this type of sub query insted of Joins......

USE [FydaDB]
    GO
    /****** Object:  StoredProcedure [dbo].[CountryManagerReport]    Script Date: 5/25/2018 2:12:42 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[CountryManagerReport] 
        @FromDate datetime,
        @Todate datetime,
        @countryId int
    AS
    BEGIN
    Select FirstName+ ' ' + MiddleName+ '' +  LastName as FullName ,Email,CNIC,((select CountryCode from Country where ID=FydaAdmin.CountryId)+ '-' +(select PhoneNo from Contacts where FydaAdminId=FydaAdmin.ID)) as Mobile,
    (ZipCode+ ' ,' +[Address]+ ' ,' + (select Name from City where ID=FydaAdmin.CityId )+ ', ' +(select Name from States where ID=FydaAdmin.StateId )+ ', ' +(select Name from Country where ID=FydaAdmin.CountryId)
     ) as [Address]
     from FydaAdmin where CreatedDateTime  BETWEEN  @FromDate  and @Todate or CountryId=@countryId 
    END