Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL数据库选择两个日期之间的数据,仅从日期向后拉,而不是月/年_Mysql_Sql_Database_Stored Procedures - Fatal编程技术网

MySQL数据库选择两个日期之间的数据,仅从日期向后拉,而不是月/年

MySQL数据库选择两个日期之间的数据,仅从日期向后拉,而不是月/年,mysql,sql,database,stored-procedures,Mysql,Sql,Database,Stored Procedures,我正在尝试使用存储过程,使用以下代码从两个日期之间的数据库中提取数据计数: select count(*) from SOMT_Development.Board_Metrics_Data bmd where bmd.Metric_Year >= startYear and bmd.Metric_Year <= endYear and Metric_Month >= startMonth and Metric_Month <= endMonth and bm

我正在尝试使用存储过程,使用以下代码从两个日期之间的数据库中提取数据计数:

 select count(*)
 from SOMT_Development.Board_Metrics_Data bmd
 where bmd.Metric_Year >= startYear and bmd.Metric_Year <= endYear and 
 Metric_Month >= startMonth and Metric_Month <= endMonth and 
 bmd.Metric_Day >= startDay and bmd.Metric_Day <= endDay and 
 bmd.Board_Metrics_ID = 1 and bmd.Value_Colour = "Red" and
 bmd.Date_Created = (select max(bmd2.Date_Created)
                      from SOMT_Development.Board_Metrics_Data bmd2
                      where bmd2.Board_Metrics_ID = bmd.Board_Metrics_ID and
                            bmd2.Metric_Year = bmd.Metric_Year and
                            bmd2.Metric_Month = bmd.Metric_Month and
                            bmd2.Metric_Day = bmd.Metric_Day
                     )) as 'red'
正如你所看到的,天数并没有上升到月底,而是上升到第四天


谢谢,

正如@niyou所说,单独比较一个日期的各个部分会遇到问题,所以你需要将它们组合成一个完整的日期。所以试着换一个

bmd.Metric_Year >= startYear and bmd.Metric_Year <= endYear and 
 Metric_Month >= startMonth and Metric_Month <= endMonth and 
 bmd.Metric_Day >= startDay and bmd.Metric_Day <= endDay
bmd.Metric\u Year>=startYear和bmd.Metric\u Year=startMonth和Metric\u Month=startDay和bmd.Metric\u Day=STR\u TO\u DATE(CONCAT\u WS('-',startYear,startMonth,startDay),“%Y-%c-%e”)和

截止日期(CONCAT_WS('-',bmd.Metric_年,bmd.Metric月,bmd.Metric日),“%Y-%c-%e”)如果你比较不同的日期部分,当你必须比较月末或年末的日期时,你总是会遇到问题。如果可能,最好比较整个日期HI@niyou我不能做整个日期,因为数据的输入不能从此格式更改。
bmd.Metric_Year >= startYear and bmd.Metric_Year <= endYear and 
 Metric_Month >= startMonth and Metric_Month <= endMonth and 
 bmd.Metric_Day >= startDay and bmd.Metric_Day <= endDay
STR_TO_DATE(CONCAT_WS('-', bmd.Metric_Year, bmd.Metric_Month, bmd.Metric_Day), '%Y-%c-%e') >= STR_TO_DATE(CONCAT_WS('-', startYear, startMonth, startDay), '%Y-%c-%e') AND
STR_TO_DATE(CONCAT_WS('-', bmd.Metric_Year, bmd.Metric_Month, bmd.Metric_Day), '%Y-%c-%e') <= STR_TO_DATE(CONCAT_WS('-', endYear, endMonth, endDay), '%Y-%c-%e')