Sql 获取包含系统日期和状态的行
我有一个如上所述的表(cycle_set),希望获取(cycleNO,month,year,start date,close date),条件是state=0,start-close date包含状态为0的下一个月内的系统日期 输出应为: 问题是,你没有办法选择下个月的结果。这两个子选项中的WHERE子句是什么 如果您提取了所有数据,然后用PHP或任何您正在使用的工具处理这些数据,那么就会容易得多Sql 获取包含系统日期和状态的行,sql,database,oracle,Sql,Database,Oracle,我有一个如上所述的表(cycle_set),希望获取(cycleNO,month,year,start date,close date),条件是state=0,start-close date包含状态为0的下一个月内的系统日期 输出应为: 问题是,你没有办法选择下个月的结果。这两个子选项中的WHERE子句是什么 如果您提取了所有数据,然后用PHP或任何您正在使用的工具处理这些数据,那么就会容易得多 SELECT cycleNO,month,year,start_date,close_date F
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...)
更新:如果要同时获取当前月份和下个月:
select cycleNO,month,year,start_date,close_date
FROM cycle_set
WHERE state=0 and sysdate between start_date and close_date
假设您有一个数据集(如示例所示):
您能给出一个要返回的示例行吗?或者详细阐述你的最后一句话。你用MySQL和Oracle标记它。您使用的是哪一个?从cycle_集合中选择cycle No、month、year、start_date、close_date,其中sysdate介于start_date和close_date之间,state=0,但如何选择下一个月?下一个月的状态将始终为零。我还必须获取下一个周期
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...)
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
)
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