2015-12-11 151 views
2

我有一個MySQL語句,它不會在PostgreSQL服務器上運行。如何將此SQL語句轉換爲PostgreSQL?

原來這裏是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; 

的目標是什麼,我想知道有多少成員年27至40歲之間的資格日期2015-12-31。 我不知道如何轉換查詢的右側部分。

+0

你得到什麼錯誤?或者發佈一些我們可以使用的示例數據 – Matt

回答

5

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

也打我吧:)好的回答和良好的'年齡'功能的使用! – Matt

+1

謝謝!很好! –