[英]SQL query not in return specific result
I'm trying to create query to not include the cars where the part_id = '1'. 我正在尝试创建查询以不包括part_id ='1'的汽车。 This works, but the car continues to show up because the car_id is associated with multiple other part_id's. 这可行,但是由于car_id与多个其他part_id相关联,所以汽车继续显示。
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_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. 从查询中返回的唯一car_id是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 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.