SQL查询显示不正确的结果

时间:2015-04-20 19:57:33

标签: sql ms-access

我正在使用Microsoft Access,我有这个SQL查询,它执行所有相关的连接:

SELECT c.ID
FROM   ((((((((Cars c
               INNER JOIN Offers
                       ON c.ID = Offers.car_id)
              INNER JOIN Users u
                      ON c.owner_id = u.ID)
             INNER JOIN City
                     ON u.city_id = City.ID)
            INNER JOIN Models
                    ON c.model_id = Models.ID)
           INNER JOIN Makes
                   ON Models.make_id = Makes.ID)
          INNER JOIN Type
                  ON Models.type_id = Type.ID) ))
WHERE  ( Offers.decision <> 3 ) 

在我的汽车表中我有1辆车,在我的优惠表中我有3个优惠同一辆车的优惠决定为2和4,由于某些原因,当我执行查询时它显示同一辆车3次喜欢它是基于3个优惠。

他们是否可以展示没有Offer.decision为3的汽车?

2 个答案:

答案 0 :(得分:1)

您不应该使用连接而是使用子查询:

SELECT * FROM Cars WHERE ID NOT IN (SELECT DISTINCT car_id FROM Offers WHERE decision <> 3)

答案 1 :(得分:1)

将您的查询简化为:

SELECT        c.ID
FROM            Cars c INNER JOIN
                     Offers ON c.ID = Offers.car_id 

WHERE(Offers.decision&lt;&gt; 3)