I have the following example generated by MS Access for generating results base on table1 without matching table2 on the IP Address columns.
SELECT
Table1.ID, Table1.IP_Address, Table1.Field1
FROM
Table1
LEFT JOIN
Table2 ON Table1.[IP_Address] = Table2.[IP Address]
WHERE
(((Table2.[IP Address]) IS NULL));
While trying to analyze "WHERE (((Table2.[IP Address]) Is Null))" I do not understand how this makes sense, as I interpret it as only return results that are NULL for table2@IP Address. My understanding of WHERE clause is like a filter mechanism for your query and NULL is blank. Can someone help me understand this counter-intuitive statement?
You already mentioned the answer:
generating results base on table1 without matching table2
You use a LEFT JOIN
, so you get all the rows from the LEFT table and matching and empty ( null
) as unmatced rows from the RIGHT table.
The unmatched rows from the RIGHT table will have Table2.[IP Address]
equal to Null
(since they are unmatching).
So the condition:
WHERE Table2.[IP Address] Is Null
will do exactly what you need:
fetch only these rows from the LEFT table that do not have a match on the RIGHT table
.
First, a more intuitive way to write the query would use NOT EXISTS
:
SELECT Table1.ID, Table1.IP_Address, Table1.Field1
FROM Table1
WHERE NOT EXISTS (SELECT 1
FROM Table2
WHERE Table1.[IP_Address] = Table2.[IP Address]
);
That said, the LEFT JOIN
method is perfectly reasonable -- and sensible too.
LEFT JOIN
keeps all the rows in the first table ( Table1
) and matching rows in the second. If there is no match, then the Table2
columns need to be filled with a value -- and for the non-matches, that value is NULL
.
The WHERE
clause is keeping only these NULL
values. Voila! It keeps the rows in Table1
that have no matching value in Table2
.
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.