2010-08-09 32 views

回答

0

您可以使用简单的“与”逻辑来实现此目的。

Select * from <people_table> 
    where ((age between 19 and 25) and 
      (current_date < to_date('2011-01-01','YYYY-MM-DD')) 
     ) 
    or ( (age between 19 and 26) and 
      (current_date >= to_date('2011-01-01','YYYY-MM-DD')) 
     ) 

当然,除非通过CURRENT_DATE,如果你的意思是SYSDATE,你必须......

Select * from <people_table> 
    where ((age between 19 and 25) and 
      (sysdate < to_date('2011-01-01','YYYY-MM-DD')) 
     ) 
    or ( (age between 19 and 26) and 
      (sysdate >= to_date('2011-01-01','YYYY-MM-DD')) 
     ) 
0

那么,在MySQL,CURDATE()将让你的当前日期。然后,你可以起诉DATEDIFF(),看看你的差异是否在你想要的范围内。 例如,

SELECT * FROM 表 WHERE DATEDIFF(年,CURDATE(),日期)> 18 AND DATEDIFF(年,CURDATE(),日期)< 26;

0

试试这个 -

select * from people_table 
    where age > 19 and age <= 
    case 
    when (getdate() < '2011-01-01') then 25 
    when (getdate() >= '2011-01-01') then 26 
    end