簡體   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