我有一个不能在PostgreSQL服务器上运行的MySQL语句.
这是原始的MySQL查询:
SELECT count(*) AS howmany FROM members WHERE member_status =1 AND member_sex = 'male' AND (YEAR( '2015-12-31' ) - YEAR( member_birthdate ) ) - ( RIGHT( '2015-12-31', 5 ) < RIGHT( member_birthdate, 5 ) ) BETWEEN 27 AND 40;
这是我的方法:
SELECT COUNT(*) AS howmany FROM members WHERE member_status =1 AND member_sex ='male' AND (EXTRACT(YEAR FROM '2015-12-31'::date) - EXTRACT(YEAR FROM member_birthdate)) BETWEEN 27 AND 40;
目标是,我想知道在2015-12-31的资格日期,有多少会员年龄在27到40岁之间.我不知道如何转换查询的右侧部分.
你可以使用AGE
功能:
SELECT COUNT(*) AS howmany FROM members WHERE member_status =1 AND member_sex ='male' AND extract(year from age(timestamp '2015-12-31', member_birthdate)) BETWEEN 27 AND 40;