1
>          cycleNO| month |   year|  start date |    close date         |state | prv month  | next mnt 
>             1      4        2012    1/4/2012     30/4/2012 23:59:59      1       3                5
>             1      5        2012    1/5/2012     31/5/2012 23:59:59      0       4                6  
>             1      6        2012    1/6/2012     30/6/2012 23:59:59      0       5                7  
>             1      7        2012    1/7/2012     31/7/2012 23:59:59      0       6                8
>             2      4        2012    1/4/2012     30/4/2012 23:59:59      1       3                5
>             2      5        2012    1/5/2012     31/5/2012 23:59:59      0       4                6  
>             2      6        2012    1/6/2012     30/6/2012 23:59:59      0       5                7  
>             2      7        2012    1/7/2012     31/7/2012 23:59:59      0       6                8

我有一个像上面一样的表(cycle_set),并且想要获取(cycleNO,month,year,start date,close date)条件为state = 0和start-close date,其中包含下个月内的系统日期状态=0。

输出应该是:

cycleNO | month | year | start date | close date 
1      5        2012    1/5/2012     31/5/2012 23:59:59
1      6        2012    1/6/2012     30/6/2012 23:59:59
2      5        2012    1/5/2012     31/5/2012 23:59:59
2      6        2012    1/6/2012     30/6/2012 23:59:59
4

3 回答 3

1
SELECT cycleNO,month,year,start_date,close_date FROM cycle_set
WHERE state=0
AND MONTH(start_date) = (SELECT month FROM cycle_set WHERE state=0 AND...)
AND MONTH(close_date) = (SELECT month FROM cycle_set WHERE state=0 AND...)

问题是,您无法选择下一个结果月。这两个子选择中的 WHERE 子句中有什么内容?

如果您提取所有数据,然后在 PHP 或您正在使用的任何工具中处理它会容易得多。

于 2012-05-10T10:59:54.913 回答
1
select cycleNO,month,year,start_date,close_date 
FROM cycle_set
WHERE state=0 and sysdate between start_date and close_date

更新:如果你想同时获得当前和下个月:

select cycleNO, month, year, start_date, close_date 
FROM cycle_set
WHERE state=0 
   and ( sysdate between start_date and close_date or --current month
         sysdate between add_months(start_date,-1)  and close_date --next_month
        )
于 2012-05-10T12:10:06.757 回答
0

假设您有一个数据集(如示例所示):

    SELECT  cycleno,month,year,start_date,close_date 
      FROM  cycle_set
     WHERE  state=0 
       AND  (   month = EXTRACT(month FROM sysdate) 
             OR month = EXTRACT(month FROM sysdate)+1
            )
  ORDER BY  cycleno, month, year, start_date
于 2012-05-10T19:31:13.640 回答