所以这是在MS Access中 - 我知道,但这就是他们所拥有的 - 如果我只设置i
,则以EMP_FID=3
返回的内部查询有效。正如最初的写作,他们只是通过DAO循环每个员工,并在那个地方传递了价值,但因为现在有超过1K的员工,这需要永远。我正在尝试对此进行优化,并在单个查询中获得所需的结果。我有以下内容:
SELECT e.ID, i.ITM, i.DUE_BY, i.TXT FROM tblEmployees as e, (SELECT tblItems.ITM, tblItems.DUE_BY, tblItems.TXT
FROM tblItems
LEFT JOIN (SELECT ITM_FID FROM tblHistory WHERE EMP_FID=e.ID) as h
ON tblItems.ID = h.ITM_FID
WHERE h.ITM_FID Is Null And Int(tblItems.DUE_BY - Now()) <= 0
ORDER BY tblItems.DUE_BY DESC) as i
查询运行,但当我知道它应该返回单个记录时,它不会返回结果。
tblItems
列出了员工需要完成的事项。
tblHistory
包含emp_fid
和itm_fid
以及完成日期的日期。
tblEmployees
有员工。
如前所述,这有效:
SELECT tblItems.ITM, tblItems.DUE_BY, tblItems.TXT
FROM tblItems
LEFT JOIN (SELECT ITM_FID FROM tblHistory WHERE EMP_FID=3) as h
ON tblItems.ID = h.ITM_FID
WHERE h.ITM_FID Is Null And Int(tblItems.DUE_BY - Now()) <= 0
ORDER BY tblItems.DUE_BY DESC
但你不知道它是哪个员工。这就是我尝试添加tblEmployees
以获取ID
并将其传递给内部查询的原因。为了获得所有员工过期的所有项目。
如果你能指出我正确的方向,我将不胜感激!
答案 0 :(得分:0)
好的,我完全想过这个。我只是对它进行了改造,它完美无缺:
SELECT tblEmployees.ID, tblItems.ITM, tblItems.DUE_BY, tblItems.TXT
FROM tblEmployees, tblItems
WHERE
tblEmployees.ID NOT IN (SELECT EMP_FID FROM tblHistory WHERE ITM_FID=tblItems.ID)
AND
tblItems.ID NOT IN (SELECT ITM_FID FROM tblHistory WHERE EMP_FID=tblEmployees.ID)
AND
Int(tblItems.DUE_BY - Now()) <=0
AND
tblEmployees.ACTV=True
ORDER BY tblItems.DUE_BY DESC