比如一条记录为:id biryear
1  1970
2  1981
3  1982查找大于 20岁的,小于 30岁的记录,并且最后输出的是 年龄。  该怎样写SQL语句呢? 

解决方案 »

  1.   

    select * from table1 where biryear between year(curdate())-30 and year(curdate())-20 
      

  2.   


    mysql> select *,year(curdate())-biryear as age from t where year(curdate())-biry
    ear between 20 and 30 //
    +------+---------+------+
    | id   | biryear | age  |
    +------+---------+------+
    |    2 | 1981    |   30 |
    |    3 | 1982    |   29 |
    +------+---------+------+
      

  3.   

    selecte * from table where year between 20 and 30 
      

  4.   

    select * from table1 
    where biryear between year(curdate())-30 and year(curdate())-20