简体   繁体   中英

How to convert this SQL-Statement to PostgreSQL?

I have a MySQL Statement which won't run on a PostgreSQL Server.

Here is the original MySQL Query:

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;

This is my approach:

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;

The Goal is, that i want to know how many Members are between 27 and 40 Years old on the qualifying date 2015-12-31. I don't know how to convert the RIGHT Part of the Query.

You can use AGE function:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM