我需要一个SQL语句来包含地址1或地址2,或者使用这两个地址来创建邮件列表

时间:2012-07-23 18:42:47

标签: sql ms-access-2007

我有一个有邮寄地址的年轻照顾者数据库。有时年轻人有第二个地址,取决于他们住在一起。有时需要将信息发送到地址1或地址2或两个地址;我用一个组合框告诉我需要使用哪个地址,即地址1,地址2或两者。

如何编写查询?这是现在的查询,我只需要它自动插入正确的地址!

SELECT tblYoungCareerDetails.Flagged
    , tblYoungCarersDetails.FirstName
    , tblYoungCarersDetails.LastName
    , tblYoungCarersDetails.Address1
    , tblYoungCarersDetails.Address2
    , tblYoungCarersDetails.Address3
    , tblGeographicalArea.QuarterStarted
    , tblYoungCarersDetails.[2ndAddress1]
    ,  tblYoungCarersDetails.[2ndAddress2]
    , tblYoungCarersDetails.[2ndAddress3]
    , tblMailOutList
    , tblYoungCarersDetails.UseAddressLabel 
FROM (tblYoungCarersDetails 
INNER JOIN tblMailOutDetails 
    ON tblYoungCarersDetails.YoungCarersID = tblMailOutDetails.YoungCarersID) 
INNER JOIN tblGeographicalArea 
    ON (tblMailOutDetails.YoungCarersID) 
    AND (tblMailOutDetails.YoungCarersID = tblGeographicalArea.YoungCarersID)
WHERE (((tblMailOutDetails.MailOutList)=Yes));

这给了我所有年轻职业的邮件详细信息,我现在如何确保他们将信息传递到正确的地址?

1 个答案:

答案 0 :(得分:0)

您可以选择第一个地址和第二个地址作为联合查询。

SELECT a.* 
    , tblGeographicalArea.QuarterStarted
    , tblMailOutDetails.MailOutList

FROM  (

(SELECT YoungCarersID
    , "Select Address1" As WhereFrom
    , t.Flagged
    , t.FirstName
    , t.LastName
    , t.Address1
    , t.Address2
    , t.Address3
    , t.UseAddressLabel 
FROM tblYoungCarersDetails  t
WHERE t.UseAddressLabel  = "Address 1" 
     OR t.UseAddressLabel  = "Both" 

UNION 
SELECT YoungCarersID
    , "Select Address2" As WhereFrom
    , t.Flagged
    , t.FirstName
    , t.LastName
    , t.[2ndAddress1]
    , t.[2ndAddress2]
    , t.[2ndAddress3]
    , t.UseAddressLabel 
FROM tblYoungCarersDetails  t
WHERE t.UseAddressLabel  = "Address 2" 
     OR t.UseAddressLabel  = "Both" ) As a

INNER JOIN tblMailOutDetails 
    ON a.YoungCarersID = tblMailOutDetails.YoungCarersID )

INNER JOIN tblGeographicalArea 
    ON tblMailOutDetails.YoungCarersID = tblGeographicalArea.YoungCarersID
WHERE tblMailOutDetails.MailOutList=Yes