如何在postgresql中找出两个参数之间的值?

时间:2011-02-06 09:18:56

标签: sql postgresql date

例如:

name | startyear | endyear 
jon    2003       2005
jake   1999       2002
blake  1997       1998
jake   1995       1996
jason  1993       1994

有没有办法回报布莱克?

我想知道杰克这两个词之间的掌柜是谁。

2 个答案:

答案 0 :(得分:2)

您可以使用between,例如:

select  yt.name
from    YourTable yt
where   TheYear between yt.startyear and yt.endyear

between是包容性的,所以这将使Blake为TheYear = 97和98。

答案 1 :(得分:2)

如果没有更多信息,这是我能给出的最佳建议:

SELECT name 
FROM your_table
WHERE startyear > (SELECT min(endyear) FROM your_table WHERE name = 'jake')
  AND endyear   < (SELECT max(startyear) FROM your_table WHERE name = 'jake')