2

假设我有一张桌子Person,我还有另外 3 Doctor, Policeman, Lawyer.... 这 3 张桌子idPerson从中获取共同的属性,如姓名 surname 等.... 如果我想找到所有医生或律师的人他们从表中的共同信息Person,我该怎么做?

我尝试过类似的东西(通过将所有组合右连接,左连接,内连接来播放)

SELECT Person.* 
FROM Person 
RIGHT JOIN doctor ON doctor.idPerson = person.idPerson 
LEFT JOIN lawyer ON lawyer.idPerson = person.idPerson
4

3 回答 3

3

除了使用连接之外,您可能会发现使用起来更简单EXISTS

SELECT col1, col2, ... coln
FROM Person
WHERE EXISTS (
    SELECT * FROM Doctor WHERE Doctor.idPerson = Person.idPerson
) OR EXISTS (
    SELECT * FROM Lawyer WHERE Lawyer.idPerson = Person.idPerson
) 
于 2012-05-15T19:43:20.090 回答
1

试试这个

SELECT Person.* 
FROM Person 
LEFT JOIN doctor ON doctor.idPerson = person.idPerson 
LEFT JOIN lawyer ON lawyer.idPerson = person.idPerson
WHERE lawyer.idPerson IS NOT NULL 
OR doctor.idPerson IS NOT NULL 
于 2012-05-15T19:43:32.030 回答
1
SELECT Person.* FROM Person 
left outer JOIN doctor ON doctor.idPerson = person.idPerson 
LEFT outer JOIN lawyer ON lawyer.idPerson = person.idPerson
where doctor.personid is not null
or laywer.personid is not null
于 2012-05-15T19:43:50.297 回答