我想要合并3张桌子。一个是人员列表,一个是人员表中每个人的计划列表,第三个是每个人的临时计划列表。我想返回一个表格,其中包含人员的一些信息,然后是他们的日程表和临时表,其中两种类型的日程表开始和结束日期符合某些标准。我尝试了以下SQL代码......
SELECT Personnel.ID, Personnel.Name, Schedules.LinkID, Schedules.BeginDate, Schedules.EndDate, Schedules.Shift, tempSchedules.LinkID, tempSchedules.BeginDate, tempSchedules.EndDate, tempSchedules.Shift
FROM ((Personnel INNER JOIN Schedules ON (Schedules.LinkID = Personnel.ID
AND ((Schedules.BeginDate BETWEEN #01-June-2014# and #30-June-2014#) OR (Schedules.EndDate BETWEEN #01-June-2014# and #30-June-2014#) OR (Schedules.BeginDate <=#01-June-2014# AND Schedules.EndDate >=#30-June-2014#))))
INNER JOIN tempSchedules ON (tempSchedules.LinkID = Personnel.ID
AND ((TempSchedules.BeginDate BETWEEN #01-June-2014# and #30-June-2014#) OR (TempSchedules.EndDate BETWEEN #01-June-2014# and #30-June-2014#) OR (TempSchedules.BeginDate <=#01-June-2014# AND TempSchedules.EndDate >=#30-June-2014#))))
ORDER BY Schedules.Shift
可以工作,除了它返回结果,其中只有TempSchedules和Schedules开始和结束日期符合条件,并将TempSchedules的列连接到Schedules列的一侧。例如,我得到以下列...
ID | Name | Schedules.LinkID | Schedules.BeginDate | Schedules.EndDate | Schedules.Shift | TempSchedules.LinkID | TempSchedules.BeginDate | TempSchedules.EndDate | TempSchedules.Shift
我希望它看起来像这样......
ID | Name | LinkID | BeginDate | EndDate | Shift
因此,我会从Schedule中获取与日期条件匹配的列,然后从TempSchedules获取符合日期条件的列。
ID | Name | LinkID | BeginDate | EndDate | Shift
01 | Bob | 52 | 01-April-2014 | 01-Dec-2014 | Days
02 | Tim | 34 | 01-Jan-2014 | 01-Aug-2014 | Days
03 | Bob | 52 | 01-Jun-2014 | 15-Jun-2014 | Swings //this is from temp schedules)
04 | Tim | 34 | 07-Jun-2014 | 28-Jun-2014 | Graves //this is from temp schedules)
我正在使用Javascript访问Microsoft Access数据库。
答案 0 :(得分:1)
考虑使用UNION ALL:类似这样的事情:
SELECT ID, Name, LinkID, BeginDate, EndDate, Shift
FROM (
SELECT Personnel.ID, Personnel.Name, Schedules.LinkID, Schedules.BeginDate, Schedules.EndDate, Schedules.Shift
FROM Personnel INNER JOIN Schedules ON (Schedules.LinkID = Personnel.ID
AND ((Schedules.BeginDate BETWEEN #01-June-2014# and #30-June-2014#) OR (Schedules.EndDate BETWEEN #01-June-2014# and #30-June-2014#) OR (Schedules.BeginDate <=#01-June-2014# AND Schedules.EndDate >=#30-June-2014#)))
union all
SELECT Personnel.ID, Personnel.Name,tempSchedules.LinkID, tempSchedules.BeginDate, tempSchedules.EndDate, tempSchedules.Shift
FROM Personnel INNER JOIN tempSchedules ON (tempSchedules.LinkID = Personnel.ID
AND ((tempSchedules.BeginDate BETWEEN #01-June-2014# and #30-June-2014#) OR (tempSchedules.EndDate BETWEEN #01-June-2014# and #30-June-2014#) OR (tempSchedules.BeginDate <=#01-June-2014# AND tempSchedules.EndDate >=#30-June-2014#)))
)
ORDER BY Shift