简体   繁体   中英

Sql statement returning no results

I am trying to return values based on the result of my access Sql statement structure. However, no results are being returned either in access or 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. 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. Disclaimer, there might be a minor syntax error - I don't have Access to test it.

Step 1, remove the "demo" criteria on your last table:

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. 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? 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. 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. 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; they are fundamental database skills.

OK, Last query, find everything about [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. If this has NULLS, then it seems some expected relationships or data fields may be missing. The query cannot "connect" the tables as you expect in your provided query. I would need feedback to provide anymore help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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