首页 文章

如果声明在mysql中的where子句中选择语句

提问于
浏览
0

我需要在select语句中将'if'放入我的where子句中 . 我正在搜索两个日期,并希望在两个日期之间提取数据 . 我需要检查年份是否不同,月份是否不同,然后是一天 . 我目前的选择陈述是:

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'

目前,如果年份和月份在开始日期和结束日期都相同,则该语句有效 . 但是,当月份/年份不同时,它无法正常工作 . 如果我输入2018-3-1和2018-4-4的日期,这是返回的数据

2018-03-29 09:46:20    green   1   no_comment  2018    3   1
  2018-03-29 09:46:20    red     1   no_comment  2018    3   2
  2018-03-29 09:46:20    white   1   no_comment  2018    3   3
  2018-03-29 09:46:20    white   1   no_comment  2018    3   4
  2018-04-04 13:25:19    green   1   no_comment  2018    4   4
  2018-04-02 13:25:30    green   1   no_comment  2018    4   2
  2018-04-03 13:25:47    green   1   no_comment  2018    4   3

请问您能告诉我如何做一个if语句,如果月份和年份不同,将会搜索本月余下的时间 .

对不起,如果这是一个简单的问题 .

谢谢

1 回答

  • 0

    IF 语句只能用于过程语法,而不能用于 where 子句中的查询语句 . 虽然你可以根据需要使用 CASE 语句 . 此外,不要认为您当前的方法有任何问题

相关问题