日期:2014-05-18  浏览次数:20570 次

求个 SQL 语句,年、月相关
1、数据库有两个字段:【年】、【月】,都是 int 类型
2、我想得到: >= 2010年5月,并且 <= 2011年12月


------解决方案--------------------
这个可以直接用条件过滤啊
大于等于2010年5月 条件就是 ((年份=2010 and 月份>=5) or 年份>2010) and (相同逻辑 <=2011年12月)
------解决方案--------------------
SQL code


--虚拟临时表
with tb1 as
(
    select 1 as id ,2009 as v_year,5 as v_month union all
    select 2,2010,12 union all
    select 3,2011,2 union all
    select 4,2010,5 union 
    select 5,2010,7 
)
select * from tb1
where v_year>=year('2010-05-01') and v_month>=MONTH('2010-05-01') 
    and 
      v_year<=year('2011-12-01') and v_month<=MONTH('2011-12-01') 
/*
id          v_year      v_month
----------- ----------- -----------
2           2010        12
4           2010        5
5           2010        7

(3 row(s) affected
*/