您没有得到结果的原因是几乎可以肯定由于表中包含的实际数据行。由于我们看不到表中的实际数据,我将为您提供一组关于如何“调试”查询的说明,并了解为什么没有显示预期结果。您基本上希望继续删除 过滤条件,直到您开始看到结果。我相信这将表明问题所在。
在这些示例中,我重新格式化了您的 SQL,使其更加用户友好。免责声明,可能存在轻微的语法错误 - 我没有访问权限来测试它。
第 1 步,删除最后一张桌子上的“演示”条件:
SELECT
Boxes.Box
,Boxes.CustRef
,Boxes.Customer
FROM
Requests
INNER JOIN [Request Boxes] ON Requests.[Request no] = [Request Boxes].[Request no]
INNER JOIN Boxes ON [Request Boxes].Box = Boxes.Box
WHERE
Requests.[Request no] = '11702'
-- Step 1: commented this criteria out
--AND Boxes.Customer = 'DEMO'
;
你得到结果了吗?如果是,这意味着 [Boxes] 表不包含指定请求编号的 customer='DEMO' 的一行或多行。如果否,请删除其他条件:
SELECT
Boxes.Box
,Boxes.CustRef
,Boxes.Customer
FROM
Requests
INNER JOIN [Request Boxes] ON Requests.[Request no] = [Request Boxes].[Request no]
INNER JOIN Boxes ON [Request Boxes].Box = Boxes.Box
-- step 2: remove the entire where clause
--WHERE
--Requests.[Request no] = '11702'
-- Step 1: commented this criteria out
--AND Boxes.Customer = 'DEMO'
;
上面的查询应该显示[Boxes]表中的所有数据,你得到结果了吗?如果是,这意味着您指定的请求编号不存在。如果否,则很可能在 [Request Boxes] 或 [Requests] 表中缺少关系。
由于 INNER JOINS 也充当过滤器,接下来您需要切换到 LEFT JOINS 以查看是否存在缺失的关系。 LEFT JOIN 的基本描述...它将允许您查看第一个表中的数据并在表无法连接的地方显示 NULL。如果您不熟悉LEFT 和INNER JOINS 的区别,我强烈建议您花大量时间彻底了解这些;他们是基本的数据库技能。
好的,最后一个查询,找到关于 [Request no] = '11702' 的所有信息。
SELECT
-- I added some new fields here to show the places the query joins on
Requests.[Request no]
-- check 1
-- if this is NULL, a relationship is missing. The database cannot connect the tables with an INNER JOIN
-- Stop, problem found
,[Request Boxes].[Request no]
-- check 2
-- if this is NULL, data is missing.
-- Stop, problem found
,[Request Boxes].Box AS [RequestBoxes.Box]
-- check 3
-- if this is NULL, a relationship is missing. The database cannot connect the tables with an INNER JOIN
-- Stop, problem found
,Boxes.Box AS [Boxes.Box]
-- check 4
-- if this is NULL, data is missing.
-- Stop, problem found
,Boxes.Customer
FROM
Requests
LEFT JOIN [Request Boxes] ON Requests.[Request no] = [Request Boxes].[Request no]
LEFT JOIN Boxes ON [Request Boxes].Box = Boxes.Box
WHERE
Requests.[Request no] = '11702'
;
此查询是否显示数据?如果是,它可能会有 NULLS。如果这有 NULLS,那么似乎某些预期的关系或数据字段可能会丢失。查询无法按照您在提供的查询中所期望的那样“连接”表。我需要反馈来提供更多帮助。