[英]If statement in where clause in mysql select statement
我需要在我的select語句的where子句中添加一個“ if”。 我正在搜索兩個日期,並希望在兩個日期之間提取數據。 我需要檢查年份是否不同,月份是否不同以及日期。 我當前的選擇語句是:
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語句,如果月份和年份不同,則將搜索該月份的其余時間。
抱歉,這是一個簡單的問題。
謝謝
IF
語句只能在過程語法中使用,而不能在where
子句的查詢語句中使用。 雖然您可以根據需要使用CASE
語句。 而且,您認為當前的方法沒有任何問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.