Reputation: 61
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:
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:
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:
Notes: Record 2 - Blank Port is Null. Record 3 should not match and not appear in query.
DONOTDELETE_FPTable
Expected Results with Query (Matching Records between two tables with Null):
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:
Upvotes: 4
Views: 3649
Reputation: 61
Here is the solution to my question that I came up with. After testing different scenarios, I found that I got the expected results from my queries. Here is the MS Access 2010 query. Please note that MS Access added the additional punctuation and I believe this was my original issue:
SQL Code as seen in MS Access:
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));
Let me know your thoughts. Thanks again for the help.
Upvotes: 2
Reputation: 97131
With the sample data you added to the question, I get the results you asked for with this query in 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);
If you do not get the expected results with that query, confirm the "blank" Port
values in both tables are really Null. If Port
is a text field, it could contain empty strings, spaces, or non-printing characters ... any of those would be difficult to distinguish from Null by visual examination. You can run a simple query on each of the tables to check.
SELECT t.Port, IsNull(t.Port) AS port_is_null
FROM Imported AS t;
In results from that query, -1 represents True
and 0 represents False
.
Upvotes: 2