问题
I am using the following query to join two tables together:
SELECT SDA.smachIPAddress,
DPP.ScanName,
DPP.pspplMSSeverity,
DPP.PatchMissing,
DPP.ScanDate
FROM patchtest_withsev DPP
INNER JOIN patchtest_withip SDA
ON DPP.ScanName =SDA.ScanName
and receiving 2351 rows of data
When I query the patchtest_withsev table for all records it only returns 99 and the patchtest_withip table only returns 99.
Can anyone see why this query is producing such a large mismatch?
回答1:
It seems that both table have several rows with same values of ScanName
.
For example:
table1:
f1 | f2
1 | a
1 | b
2 | c
2 | c
table2:
f1 | f2
1 | a
1 | b
2 | c
table1 INNER JOIN table2 ON table1.f1 = table2.f1 gives:
table1.f1 | table1.f2 | table2.f1 | table2.f2
1 | a | 1 | a
1 | a | 1 | b
1 | b | 1 | a
1 | b | 1 | b
2 | c | 2 | c
2 | c | 2 | c
To avoid completely duplicate rows in result try using DISTINCT
.
回答2:
Do you have duplicate values for ScanName in either table? If so, then SQL will return one row for every match, which inflates the number of rows very quickly. (e.g., if there are 3 rows with a given value in the first table and 6 rows with that value in the second, SQL will return 18 rows).
回答3:
If there are multiple records in patchtest_withip
that match the criteria you are joining on from patchtest_withsev
you could get results like that. Based on the results you are getting I would say there are 23-24 records in patchtest_withip
that have the same ScanName
as every record in patchtest_withsev
回答4:
If there are 99 entries in each table, assuming there were no unique values for scanName, you could potentially have 99x99=9801 entries in your results.
If you are getting true duplicates, try SELECT DISTINCT.
来源:https://stackoverflow.com/questions/10939090/inner-join-returning-large-numbers-of-duplicates