MS-SQL问题 - 只能在选择列表中指定一个表达式

时间:2021-07-11 19:15:37

I write the following query:

我写了以下查询:

select 
   id, 
   (select NameEn from [Campaign] where id=CampaignId) as CampaignName,    
   createdDate, 
   (select Name, IdNo, Email, MobileNo from [Members] where id=MemberId) 
from 
   Transactions

and error occurs:
"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."

并发生错误:“当EXISTS未引入子查询时,只能在选择列表中指定一个表达式。”

How can I rewrite the SQL to make it correct?

如何重写SQL以使其正确?

1 个解决方案

#1


You need to use proper (inner|left|...) join syntax.

您需要使用正确的(内部|左| ...)连接语法。

Something like:

  select 
      t.id, 
      c.NameEn,
      t.createdDate,
      m.Name,
      m.IdNo,
      m.Email,
      m.MobileNo
  from
      [Transactions] t 
      inner join [Campaign] c on c.id = t.CampaignId
      inner join [Members] m on m.id = t.MemberId

Also, in your original code, one of

另外,在您的原始代码中,其中一个

  select NameEn from [Campaign] where id=CampaignId

or

  select Name,IdNo,Email,MobileNo from [Members] where id=MemberId

might be returning more than one row for each row of [Transactions], which would be illegal.

对于[Transactions]的每一行,可能会返回多行,这将是非法的。

#1


You need to use proper (inner|left|...) join syntax.

您需要使用正确的(内部|左| ...)连接语法。

Something like:

  select 
      t.id, 
      c.NameEn,
      t.createdDate,
      m.Name,
      m.IdNo,
      m.Email,
      m.MobileNo
  from
      [Transactions] t 
      inner join [Campaign] c on c.id = t.CampaignId
      inner join [Members] m on m.id = t.MemberId

Also, in your original code, one of

另外,在您的原始代码中,其中一个

  select NameEn from [Campaign] where id=CampaignId

or

  select Name,IdNo,Email,MobileNo from [Members] where id=MemberId

might be returning more than one row for each row of [Transactions], which would be illegal.

对于[Transactions]的每一行,可能会返回多行,这将是非法的。