简体   繁体   English

Sql语句返回没有结果

[英]Sql statement returning no results

I am trying to return values based on the result of my access Sql statement structure. 我试图根据我的访问Sql语句结构的结果返回值。 However, no results are being returned either in access or VB.Net. 但是,在访问或VB.Net中没有返回任何结果。 Now being a new user to both access and vb.Net, It is something I have coded incorrectly and would be grateful if someone could point out my error. 现在成为访问和vb.Net的新用户,这是我编码错误的东西,如果有人能指出我的错误,将不胜感激。 If you need any further info, I will be only to happy to oblige. 如果您需要任何进一步的信息,我将很乐意承担责任。 Many thanks 非常感谢

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'));

The reason you are not getting results is almost certainly due to the actual data rows contained in your tables. 您没有获得结果的原因几乎可以肯定是由于表中包含的实际数据行。 Since we cannot see the actual data in the tables, I will give you a set of instructions on how to "debug" the query and learn why the expected results are not shown. 由于我们无法在表格中看到实际数据,因此我将为您提供一组有关如何“调试”查询并了解未显示预期结果的说明。 You basically want to keep removing filtering criteria until you start seeing results. 您基本上希望继续删除过滤条件,直到您开始看到结果。 I believe this will indicate where the problem lies. 我相信这将表明问题所在。

I reformatted your SQL to be more user-friendly in these examples. 在这些示例中,我重新格式化了SQL以使其更加用户友好。 Disclaimer, there might be a minor syntax error - I don't have Access to test it. 免责声明,可能存在轻微的语法错误 - 我没有Access来测试它。

Step 1, remove the "demo" criteria on your last table: 第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'
;

Do you get results? 你有结果吗? If Yes, this means [Boxes] table does not contain one or more rows for customer='DEMO' for the specified request number. 如果是,则表示[Boxes]表不包含指定请求编号的customer ='DEMO'的一行或多行。 If No, remove another criteria: 如果否,请删除其他条件:

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'
;

The above query should show all data in the [Boxes] table, do you get results? 上面的查询应该显示[Boxes]表中的所有数据,你得到的结果是什么? If Yes, this means the request number you are specifying does not exist. 如果是,则表示您指定的请求编号不存在。 If No, then it seems very likely that relationships are missing in either [Request Boxes] or [Requests] tables. 如果否,则很可能在[请求框]或[请求]表中缺少关系。

Since INNER JOINS also act as filters, next you will need to switch to LEFT JOINS to see if there are missing relationships. 由于INNER JOINS也充当过滤器,接下来你需要切换到LEFT JOINS以查看是否缺少关系。 A basic description of LEFT JOIN ... it will allow you to see data from the first table and show NULLs where tables cannot be connected. LEFT JOIN基本描述......它允许您查看第一个表中的数据,并显示无法连接表的NULL。 If you are not familiar with the differences of LEFT and INNER JOINS , I would strongly suggest to invest a lot of time learning these thoroughly; 如果你不熟悉LEFTINNER JOINS的差异,我强烈建议你花很多时间彻底学习这些。 they are fundamental database skills. 它们是基础数据库技能。

OK, Last query, find everything about [Request no] = '11702'. 好的,上次查询,找到有关[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'
;

Does this query show data? 此查询是否显示数据? If yes, it will likely have NULLS. 如果是,它可能会有NULLS。 If this has NULLS, then it seems some expected relationships or data fields may be missing. 如果这有NULLS,那么似乎某些预期的关系或数据字段可能会丢失。 The query cannot "connect" the tables as you expect in your provided query. 查询无法在您提供的查询中按预期“连接”表。 I would need feedback to provide anymore help. 我需要反馈以提供更多帮助。

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

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