开发者

Simple SQL problem, disambiguating common attributes

I have a database with a table Person, with attributes ID, Name and Age. I also have a table WorksFor, with attributes ID1 and ID2, where ID1 works for ID2

I want to list the names of all people who work for somebody at least 2 years younger than them. 开发者_C百科 How do I do this?


Assuming ID1 WorksFor ID2...

SELECT DISTINCT
   CubeRat.ID,
   CubeRat.Name,
   CubeRat.Age
FROM
   WorksFor W
   INNER JOIN PERSON Boss ON W.ID2 = Boss.ID
   INNER JOIN PERSON CubeRat ON W.ID1 = CubeRat.ID
WHERE
   CubeRat.Age >= (Boss.Age + 2)


Assuming that id1 and id2 are fields in one table where id1 - manager's id, id2 - worker id

SELECT p.name 
FROM Person p
WHERE p.age > ((PRIOR p.age) - 2)
START WITH p.id2 = *sombody's id*
CONNECT BY p.id1 = PRIOR p.id2

Oracle notation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜