我正在尝试根据创建记录的月份选择年份值:
如果月份是一月起,我应该得到去年 如果月份是四月起,我应该得到今年
SELECT IF ((MONTH(Application.created) > 3), (YEAR(Application.created)), (YEAR(Application.created, INTERVAL -1 YEAR))) FROM Table
我正在尝试根据创建记录的月份选择年份值:
如果月份是一月起,我应该得到去年 如果月份是四月起,我应该得到今年
SELECT IF ((MONTH(Application.created) > 3), (YEAR(Application.created)), (YEAR(Application.created, INTERVAL -1 YEAR))) FROM Table
您可以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