Sql语句返回没有结果

时间:2013-12-27 15:12:14

标签: sql vb.net visual-studio-2010 visual-studio ms-access

我试图根据访问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'));

1 个答案:

答案 0 :(得分:2)

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

我在这些示例中重新格式化了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,那么似乎某些预期的关系或数据字段可能会丢失。查询无法在您提供的查询中按预期“连接”表。我需要反馈才能提供帮助。

相关问题