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

时间:2009-07-20 08:37:56

标签: sql sql-server tsql

我写了以下查询:

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

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

如何重写SQL以使其正确?

1 个答案:

答案 0 :(得分:2)

您需要使用正确的(inner | left | ...)联接语法。

类似的东西:

  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

另外,在您的原始代码中,

之一
  select NameEn from [Campaign] where id=CampaignId

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

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