当前位置:  开发笔记 > 后端 > 正文

如何将此SQL语句转换为PostgreSQL?

如何解决《如何将此SQL语句转换为PostgreSQL?》经验,为你挑选了1个好方法。

我有一个不能在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岁之间.我不知道如何转换查询的右侧部分.



1> Lukasz Szozd..:

你可以使用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;

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有