0

我有一个有邮寄地址的年轻照顾者数据库。有时年轻人有第二个地址,这取决于他们与谁住在一起。有时需要将信息发送到地址 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));

这使我所有年轻职业的邮寄详细信息都得到了一定程度的服务,我现在如何确保他们将信息发送到正确的地址?

4

1 回答 1

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
于 2012-07-23T20:32:13.440 回答