2013-02-23 58 views
1

我有兩個表。計算年齡在內部查詢然後與值比較

  1. fbuser(facebook_ID,DATE_OF_BIRTH) - facebook_id是主鍵
  2. 牆(wall_ID,facebook_ID,public_view) - facebook_id是外鍵

我想找到誰擁有牆面設置爲用戶公衆觀點,年齡超過24歲?

Select f.facebook_ID from fbuser f, wall w 
WHERE 
    (w.public_view='y') 
AND 
    (f.facebook_ID=w.facebook_ID) 
AND 
    (select FLOOR(MONTHS_BETWEEN(CURRENT_DATE, f.date_of_birth)/12) 
    as person_age from fbuser where person_age >=24; 

回答

1
SELECT 
    f.facebook_ID 
FROM fbuser f, wall w 
WHERE 
    (f.facebook_ID=w.facebook_ID) 
AND 
    (w.public_view='y') 
AND 
    ( (MONTHS_BETWEEN(CURRENT_DATE, f.date_of_birth)/12) >=24);