0

我有那两张桌子

Period (idPeriod, startDate, endDate)
Person (idPerson, name, idPeriod,...otherInfos)

我们可以在许多时期准时拥有一个人。我的实际查询是返回一个最近 startDate 的人

SELECT * FROM person p INNER JOIN period per on per.idPeriod = org.idPeriod
where per.endDate = (SELECT MAX(per.startDate) from per) group by name;

如何检索人员列表。如果此人存在不止一次,则返回具有最近 startDate 的那个。

4

2 回答 2

1

尝试这个:

SELECT p.name, p.idPerson, MAX(per.startDate) 
FROM person p LEFT JOIN period per on per.idPeriod = org.idPeriod
GROUP BY  p.name, p.idPerson
于 2013-07-24T21:54:21.517 回答
0
SELECT name, idPerson
FROM Person 
 INNER JOIN Period 
 ON Person.idPeriod = Period.idPeriod
WHERE Period.startDate = 
(
  SELECT MIN(startDate)
  FROM Period AS Per 
   INNER JOIN Person as P
   ON P.id = Per.id
  WHERE P.name = Person.name
)

摆弄

于 2013-07-24T21:53:38.177 回答