试图在Select Statement中加入IIF

时间:2017-04-28 16:54:31

标签: ms-access iif

尝试在从MS Access SQL语句中选择数据时合并IIF。

如果我在数据库中为每个人提供了两个地址,并且有一个密钥告诉您要使用哪个地址,如何告诉SQL语句根据该密钥使用地址1或地址2?这是为了创建一个可以在报告中使用的查询。

我已经尝试了这个,但它在第一个IIF上引发了一个错误:

SELECT MAILINGTABLE.MailAutoNumber, MAILINGTABLE.MailLastName, MAILINGTABLE.MailFirstName, 
  IIF(MailNewsletter1or2=1) MAILINGTABLE.MailAddr1 as AD1, 
  IIF(MailNewsletter1or2=1) MAILINGTABLE.MailAddr2 as AD2, 
  IIF(MailNewsletter1or2=1) MAILINGTABLE.MailCity as city, 
  IIF(MailNewsletter1or2=1) MAILINGTABLE.MailState as State, 
  IIF(MailNewsletter1or2=1 MAILINGTABLE.MailZip as Zip, 
  IIF(MailNewsletter1or2=2) MAILINGTABLE.Addr2Addr1 as AD1,
  IIF(MailNewsletter1or2=2) MAILINGTABLE.Addr2Addr2 as AD2, 
  IIF(MailNewsletter1or2=2) MAILINGTABLE.Addr2City as City, 
  IIF(MailNewsletter1or2=2) MAILINGTABLE.Addr2State as State, 
  IIF(MailNewsletter1or2=2) MAILINGTABLE.Addr2Zip as Zip, 
  MAILINGTABLE.MailSalutation, MAILINGTABLE.[232HelpFundraiser], 
  MAILINGTABLE.InformalSalutation, MAILINGTABLE.[232HelpFundraiser1or2]
FROM MAILINGTABLE
WHERE (((MAILINGTABLE.[232HelpFundraiser])=Yes))
ORDER BY MAILINGTABLE.MailLastName, MAILINGTABLE.MailFirstName;

1 个答案:

答案 0 :(得分:0)

IIf()语法都错了。应该是这样的:
IIf(this condition true, use this field, else use other field)
如果要单独为每个地址行进行此确定,则需要其中的5个表达式。

另外建议单个IIf():

IIf(MailNewsletter1or2 = 1, MailAddr1 & Chr(13) & Chr(10) &  MailAddr2 & Chr(13) & Chr(10) & MailCity & ", " & MailState & " " & MailZip,  
Addr2Addr1 & Chr(13) & Chr(10) & Addr2Addr2 & Chr(13) & Chr(10) Addr2City & ", " & Addr2State & " " & Addr2Zip) AS Address