1

我正在尝试根据创建记录的月份选择年份值:

如果月份是一月起,我应该得到去年 如果月份是四月起,我应该得到今年

SELECT IF ((MONTH(Application.created) > 3), (YEAR(Application.created)), (YEAR(Application.created, INTERVAL -1 YEAR))) FROM Table
4

1 回答 1

2

您可以interval从年份值中减去 1(不需要)。另外,我之所以使用CASE是因为我在所有括号中都迷路了:)

SELECT
  CASE WHEN MONTH(Application.created) > 3 THEN YEAR(Application.created)
       ELSE YEAR(Application.created) - 1
  END
FROM Table

编辑:

SELECT IF ((MONTH(Application.created) > 3), (YEAR(Application.created)), (YEAR(Application.created) - 1)) FROM Table
于 2013-05-21T13:38:29.100 回答