我在Access中创建了一个报告,我编写了一个从中获取记录的查询 多个表格如下:
SELECT BuildingDetails.*, Contractors.Item, ActionDetails.ActionType
FROM Contractors
INNER JOIN (BuildingDetails
INNER JOIN (ActionDetails
INNER JOIN DormData ON ActionDetails.ActionID = DormData.ActionID)
ON BuildingDetails.BuildingID = DormData.BuildingID)
ON Contractors.ID = DormData.ItemID;
现在我想要的只是查询检索actiontype=repair
或actionid=1
。我们有两个actontype
“修复”和“替换”。
答案 0 :(得分:1)
我已经重新格式化你查询了一点来结束它。您还没有指定过滤器的数据,但根据您所说的内容,我会使用以下内容
SELECT BuildingDetails.*,
Contractors.Item,
ActionDetails.ActionType
FROM Contractors
INNER JOIN DormData ON Contractors.ID = DormData.ItemID
INNER JOIN ActionDetails ON DormData.ActionID = ActionDetails.ActionID
INNER JOIN BuildingDetails ON DormData.BuildingID = BuildingDetails.BuildingID
WHERE ActionDetails.ActionType = 'Repair' OR ActionID=1
如果ActionID是一个将ActionID(1)与ActionType('Repair')相关联的查阅列,那么您不需要或者可以坚持WHERE子句中的一个或其他条件。
希望这有帮助。
答案 1 :(得分:0)
将其添加到select语句的末尾以解决问题:
where actiondetails.actiontype = 'repair' or actiondetails.actionid = 1
答案 2 :(得分:0)
我怀疑你只需要使用actiontype = 'repair'
进行过滤(我进一步猜测ActionID
是一个自动编号,你只有偶然的{ActionID = 1, actiontype = 'repair'}
行...但这可能是推断太远了:))
我很惊讶@David Steele的答案适用于Access(ACE,Jet,无论如何),因为他从JOIN
子句中删除了括号(但如果确实如此 - 建议链接表 - 那么你应该“接受”那个答案)。但我也可以抵制“将它们整理起来”,以便ON
条款接近表名:
SELECT BuildingDetails.*, Contractors.Item, ActionDetails.ActionType
FROM ((DormData
INNER JOIN Contractors
ON Contractors.ID = DormData.ItemID)
INNER JOIN BuildingDetails
ON BuildingDetails.BuildingID = DormData.BuildingID)
INNER JOIN ActionDetails
ON ActionDetails.ActionID = DormData.ActionID
WHERE ActionDetails.ActionType = 'repair';