我有两张桌子:
1)person(person_id,person_name)
2)汽车(car_id,person_id)
我想得到所有人的名字都没有重复的汽车。
这就是我的想法:
SELECT person.person_name, cars.person_id
FROM cars
INNER JOIN person
ON person.person_id=cars.person_id
但我不想要重复,所以我需要使用类似的东西来加入它:
SELECT DISTINCT person_id FROM cars
答案 0 :(得分:4)
select person_name from person
where person_id in ( select person_id from cars )
答案 1 :(得分:0)
SELECT DISTINCT person.person_name, person.person_id
FROM cars
INNER JOIN person
ON person.person_id=cars.person_id
虽然可能有更多高性能的替代品。
答案 2 :(得分:0)
试试这个:
SELECT p.person_id, p.person_name
FROM person p
WHERE EXISTS
(
SELECT 1
FROM cars c
WHERE c.person_id = p.person_id
)
答案 3 :(得分:0)
SELECT p.person_id AS person_id, p.name AS name FROM person p, cars c
WHERE p.person_id = c.person_id
GROUP BY b.brand_id
答案 4 :(得分:-1)
SELECT DISTINCT(person.person_name)
FROM person, cars
WHERE person.person_id = cars.person_id