【问题标题】:Sql statement returning no resultssql语句不返回结果
【发布时间】:2014-01-15 04:18:39
【问题描述】:

我正在尝试根据我的访问 Sql 语句结构的结果返回值。但是,在 access 或 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'));

【问题讨论】:

  • 也许没有与查询匹配的数据?查询中似乎有很多不必要的括号。也许尝试删除 FROM 子句中的那些?
  • 大卫,我知道那里有数据并且该查询来自访问。谢谢
  • 如果删除 Boxes.Customer 上的条件会发生什么?它显示什么吗?
  • @user1532468:删除WHERE 子句中的条件(一次一个),看看返回什么结果。 JOINed 表也一样,一次一张。这是一个非常小查询,不需要做很多调试。
  • 我根本不使用 MS Access,但我会尽力提供帮助。 [Request No] 字段似乎使用字符串“[Request no] = '11702'”来表示数字数据。你能像这样尝试一个实际的数字比较吗? "[Request no] = 11702",不带单引号。

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


【解决方案1】:

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

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

【讨论】:

  • 非常感谢大卫。超越使命召唤。新年快乐
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 2016-01-16
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多