简体   繁体   中英

SQL query not in return specific result

I'm trying to create query to not include the cars where the part_id = '1'. This works, but the car continues to show up because the car_id is associated with multiple other part_id's.

Here's my query:

SELECT
    distinct car.car_id, 
             part.description
FROM car
JOIN car_parts part on (car.car_id = part.car_id)
WHERE part.part_id <> '1'

My table: car_parts

Car ID | Part ID
   1        1
   1        3
   1        4
   2        2
   2        5
   2        7
   3        1
   3        4
   3        9
   4        1

The only car_id that should be returned from the query is car_id = 2.

SELECT distinct car.car_id, part.description
FROM car
JOIN car_parts part on (car.car_id = part.car_id)
WHERE part.part_id <> '1'
AND car.car_id not in ( select car_id from part where part = 1 )

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