sql表达式将查询替换为sql生成而不匹配查询

时间:2013-07-10 14:39:14

标签: sql ms-access nested ms-access-2003

我有两个查询,其中qryAvailability1返回被阻止预订的日期,而qryAvailability2在任何预订发生前生成完全可用的日期。

我将它们组合在一个最终的“无匹配”查询中,以定义可用的预订日期:

qryAvailability1:

SELECT tblReservations.PropertyID, tblDates.Date
FROM tblReservations, tblDates
WHERE (((tblDates.Date) Between [tblReservations]![CheckIn] And [tblReservations]![CheckOut]));

qryAvailability2:

SELECT tblProperties.PropertyID,tblDates.Date FROM tblProperties,tblDates;

最终“无匹配”查询:

SELECT qryAvailability2.PropertyID, qryAvailability2.Date
FROM qryAvailability2 LEFT JOIN qryAvailability1 ON (qryAvailability2.Date=qryAvailability1.Date) AND (qryAvailability2.PropertyID=qryAvailability1.PropertyID)
WHERE (((qryAvailability1.Date) Is Null))
ORDER BY qryAvailability2.PropertyID, qryAvailability2.Date;

有没有办法在1个查询中使用单个查询语句而不是3个?

换句话说,我需要用生成它们的sql语句替换对qryAvailability1qryAvailability2的引用(无论我尝试什么都不起作用)。

1 个答案:

答案 0 :(得分:1)

假设您的最终查询有效(我还没有检查过),那么要将所有三个结合起来:

SELECT qryAvailability2.PropertyID, qryAvailability2.Date
FROM (
SELECT tblProperties.PropertyID, tblDates.Date FROM tblProperties, tblDates
) qryAvailability2 LEFT JOIN (
SELECT tblReservations.PropertyID, tblDates.Date
FROM tblReservations, tblDates
WHERE (((tblDates.Date) Between [tblReservations]![CheckIn] And [tblReservations]![CheckOut]))
) qryAvailability1 ON (qryAvailability2.Date=qryAvailability1.Date) AND (qryAvailability2.PropertyID=qryAvailability1.PropertyID)
WHERE (((qryAvailability1.Date) Is Null))
ORDER BY qryAvailability2.PropertyID, qryAvailability2.Date;