繁体   English   中英

Sql语句返回没有结果

[英]Sql statement returning no results

我试图根据我的访问Sql语句结构的结果返回值。 但是,在访问或VB.Net中没有返回任何结果。 现在成为访问和vb.Net的新用户,这是我编码错误的东西,如果有人能指出我的错误,将不胜感激。 如果您需要任何进一步的信息,我将很乐意承担责任。 非常感谢

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') 
                  AND ((Boxes.Customer) = 'DEMO'));

您没有获得结果的原因几乎可以肯定是由于表中包含的实际数据行。 由于我们无法在表格中看到实际数据,因此我将为您提供一组有关如何“调试”查询并了解未显示预期结果的说明。 您基本上希望继续删除过滤条件,直到您开始看到结果。 我相信这将表明问题所在。

在这些示例中,我重新格式化了SQL以使其更加用户友好。 免责声明,可能存在轻微的语法错误 - 我没有Access来测试它。

第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]表中的所有数据,你得到的结果是什么? 如果是,则表示您指定的请求编号不存在。 如果否,则很可能在[请求框]或[请求]表中缺少关系。

由于INNER JOINS也充当过滤器,接下来你需要切换到LEFT JOINS以查看是否缺少关系。 LEFT JOIN基本描述......它允许您查看第一个表中的数据,并显示无法连接表的NULL。 如果你不熟悉LEFTINNER 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,那么似乎某些预期的关系或数据字段可能会丢失。 查询无法在您提供的查询中按预期“连接”表。 我需要反馈以提供更多帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM