简体   繁体   中英

Inner Join returns all record

I have a three tables

  1. tblVegetationClass (VegetationClassID is a column Name)
  2. tblVegTypeVegFormationLink
  3. tblVegetationType

I have the following query

SELECT   dbo.tblVegetationType.VegTypeCode,  
    dbo.tblVegetationType.VegTypeName,dbo.tblVegetationClass.VegetationClassID,
    dbo.tblVegTypeVegFormationLink.VegetationFormationID
FROM dbo.tblVegetationType 
    INNER JOIN dbo.tblVegTypeVegFormationLink
    ON dbo.tblVegetationType.VegTypeID = dbo.tblVegTypeVegFormationLink.VegTypeID
    INNER JOIN dbo.tblVegetationClass
    ON dbo.tblVegetationType.VegetationClassID=dbo.tblVegetationClass.VegetationClassID
    INNER JOIN tblCMAVegTypeLink 
    ON dbo.tblVegetationType.VegTypeID = dbo.tblCMAVegTypeLink.VegTypeID
WHERE   dbo.tblVegetationType.PercentageCleared >= 50 
    AND dbo.tblVegTypeVegFormationLink.VegetationFormationID = 313
    AND dbo.tblCMAVegTypeLink.CMAID = 4 
    AND (dbo.tblVegetationType.EffectiveDateTo IS NULL)

The query returns

VegTypeCode VegTypeName VegetationClassID   VegetationFormationID
HU532       Coastal     40                   313
HU591       Paperbark   39                   313
HU633       Swamp       39                   313
HU635       Swamp       40                   313

在此处输入图片说明

I am expecting the query only returns record 2 and 3 or 1 and 4.

Inner join is not matching the tblVegetationType.VegetationClassID=tblVegetetationClass.VegetationClassID

Can you please correct me what i did wrong in my query ?

Your WHERE clause does not constrain tblVegetationClass in any way, so it will allow all such records through. If you were to add

AND dbo.tblVegetationClass.ID = 1

(or something along those lines) you would be restricting which vegetation class was returned by the query.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM