如何在WHERE子句中使用age()函数

时间:2019-04-05 10:46:42

标签: postgresql

我在哪里使用了年龄函数,但我不知道为什么它不起作用

select t1.id,date_part('year',age(t1.tdate,t2.birthday)) as age,t1.tdate,t2.birthday 
FROM table t1
LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE date_part('year',age(t1.tdate,t2.birthday))  >= 10

此结果不正确

+-----------+-----+------------+------------+
|    id     | age |   tdate    |  birthday  |
+-----------+-----+------------+------------+
| 000327702 |   4 | 2017-01-05 | 2012-11-12 |
| 000337682 |   5 | 2019-02-07 | 2013-09-19 |
| 000337682 |   5 | 2018-09-20 | 2013-09-19 |
| 000337682 |   5 | 2019-04-04 | 2013-09-19 |
| 000264410 |  10 | 2012-09-12 | 2001-10-07 |
| 000383791 |  10 | 2017-07-19 | 2007-01-06 |
| 000221098 |  11 | 2016-07-29 | 2004-09-12 |
| 000342658 |  11 | 2014-02-12 | 2002-06-20 |
| 000234369 |  11 | 2017-06-12 | 2005-09-06 |
| 000330417 |  12 | 2013-02-21 | 2000-06-15 |
+-----------+-----+------------+------------+

这是预期的结果

+-----------+-----+------------+------------+
|    id     | age |   tdate    |  birthday  |
+-----------+-----+------------+------------+
| 000264410 |  10 | 2012-09-12 | 2001-10-07 |
| 000383791 |  10 | 2017-07-19 | 2007-01-06 |
| 000221098 |  11 | 2016-07-29 | 2004-09-12 |
| 000342658 |  11 | 2014-02-12 | 2002-06-20 |
| 000234369 |  11 | 2017-06-12 | 2005-09-06 |
| 000330417 |  12 | 2013-02-21 | 2000-06-15 |
+-----------+-----+------------+------------+

1 个答案:

答案 0 :(得分:0)

有效

age(t1.tdate,t2.birthday) >= interval '10 year'