4

I've been having a few problem developing a query in MS Access 2010. I have not been able to find a solution on the net as of yet but I feel that I'm really close. Here is what I'm trying to do:

  1. I have two tables that I'm trying to compare and find like items. Table A is called "DONOTDELETE_FPTable". Table B is called "Imported".
  2. I'm comparing the same columns in both tables. IP, QID and PORT.
  3. My goal is to find like records in the Imported table that occur in the DONOTDELETE_FPTable.
  4. I have a Left Join setup from the DONOTDELETE_FPTable to the Import table on all columns mentioned above.

Problem: I have a situation where a record may have a null value in the column PORT in both tables. I need the query to return:

  1. All matching records between the two tables that have the same data in all columns
  2. AND any matching record that has a matching IP, QID and a possible null in the PORT column between the two tables.

Here is the SQL that I have at this point using the "LIKE" criteria. I removed an data where I'm trying to test for the null since I can't get it to work. Thanks for the help ahead of time.

SQL Statement:

SELECT DONOTDELETE_FPExcept.*
FROM
    DONOTDELETE_FPExcept
    LEFT JOIN Imported
    ON
            (DONOTDELETE_FPExcept.Port = Imported.PORT)
        AND (DONOTDELETE_FPExcept.QID = Imported.QID)
        AND (DONOTDELETE_FPExcept.IP = Imported.IP)
WHERE
        (((Imported.IP) Like [DONOTDELETE_FPExcept].[ip])
    AND ((Imported.qid) Like [DONOTDELETE_FPExcept].[QID])
    AND ((Imported.PORT) Like [DONOTDELETE_FPExcept].[Port]));

Here are the is some example data in the table.IP, QID and Port are the columns:

Import Table:

  • Record 1: IP: 10.1.1.1 / QID: 225 / Port: 80
  • Record 2: IP: 10.1.1.1 / QID: 111 / Port:
  • Record 3: IP: 10.1.1.5 / QID: 999 / Port: 8080
  • Record 4: IP: 10.1.1.5 / QID: 999 / Port:

Notes: Record 2 - Blank Port is Null. Record 3 should not match and not appear in query.

DONOTDELETE_FPTable

  • Record 1: IP: 10.1.1.1 / QID: 225 / Port: 80
  • Record 2: IP: 10.1.1.1 / QID: 111 / Port:

Expected Results with Query (Matching Records between two tables with Null):

  • Record 1: IP: 10.1.1.1 / QID: 225 / Port: 80
  • Record 2: IP: 10.1.1.1 / QID: 111 / Port:

Actually query result (Issue - Null record is missing)

  • Record 1: IP: 10.1.1.1 / QID: 225 / Port: 80

  • Record 2: IP: 10.1.1.5 / QID: 999 / Port:

4

2 に答える 2

2

これが私が思いついた私の質問に対する解決策です。さまざまなシナリオをテストした後、クエリから期待どおりの結果が得られることがわかりました。これは、MS Access 2010 クエリです。MS Access が追加の句読点を追加したことに注意してください。これが私の最初の問題だと思います。

MS Access で表示される SQL コード:

SELECT Imported.ip, Imported.qid, Imported.port

FROM

DONOTDELETE_FPExcept 
LEFT JOIN Imported 
ON 
     (DONOTDELETE_FPExcept.QID = Imported.QID) 
     AND (DONOTDELETE_FPExcept.IP = Imported.IP)

WHERE 
     (((Imported.ip)=DONOTDELETE_FPExcept.IP)       And  
     (Imported.qid)=DONOTDELETE_FPExcept.qid)       And
     ((Imported.port)=DONOTDELETE_FPExcept.Port))   Or 
     (((Imported.port) Is Null)                     And 
     ((DONOTDELETE_FPExcept.port) Is Null));

あなたの考えを聞かせてください。助けてくれてありがとう。

于 2013-08-20T14:44:01.427 に答える
2

質問に追加したサンプル データを使用して、Access 2007 の次のクエリで求められた結果を取得します。

SELECT
    d.Record_id AS d_id,
    d.IP,
    d.QID,
    d.Port,
    i.Record_id AS i_id
FROM
    DONOTDELETE_FPExcept AS d
    LEFT JOIN Imported AS i
    ON (d.QID = i.QID) AND (d.IP = i.IP)
WHERE
        d.Port = i.Port
    OR (d.Port Is Null AND i.Port Is Null);

そのクエリで期待した結果が得られない場合はPort、両方のテーブルの「空白」値が実際に Null であることを確認してください。がテキスト フィールドの場合Port、空の文字列、スペース、または印刷されない文字が含まれている可能性があります。これらはいずれも、目視検査では Null と区別するのが困難です。各テーブルで簡単なクエリを実行して確認できます。

SELECT t.Port, IsNull(t.Port) AS port_is_null
FROM Imported AS t;

そのクエリの結果では、-1 は を表しTrue、0 は を表しFalseます。

于 2013-08-16T21:48:32.383 に答える