我有以下表格评估 -
SLNO EID Period_From Period_To
1 101 2017-06-01 2017-11-14
2 102 2017-07-01 2017-09-30
3 103 2017-05-01 2017-07-31
如果Period_To和currentdate等于75天,那么row应该返回1 else 0我在下面使用查询 -
SELECT SLNO,EID, Period_From,Period_To,(CASE WHEN (PERIOD_TO = (select DATE_ADD(PERIOD_TO,INTERVAL 75 DAY))) THEN 1 ELSE 0 END) AS ASSESSMENT_ENABLE from assessment;
我的结果为
SLNO EID Period_From Period_To ASSESSMENT_ENABLE
1 101 2017-06-01 2017-11-14 0
2 102 2017-07-01 2017-09-30 0
3 103 2017-05-01 2017-07-31 0
我的结果错了。请帮我。
答案 0 :(得分:0)
我认为你不需要子查询。试试这个对我有用
SELECT SLNO,EID, Period_From,Period_To,
(CASE WHEN (PERIOD_TO = DATE_FORMAT(CURDATE(), '%Y-%m-%d') - INTERVAL 75 DAY) THEN 1 ELSE 0 END)
AS ASSESSMENT_ENABLE
答案 1 :(得分:0)
使用CURRENT_DATE
和DATEDIFF
函数的简短解决方案:
SELECT
SLNO, EID, Period_From, Period_To,
IF(DATEDIFF(CURRENT_DATE(), date_to) = 75, 1, 0) AS ASSESSMENT_ENABLE
FROM
ASSESSMENT
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
答案 2 :(得分:0)
你可以试试这个 -
select
SLNO,
EID,
Period_From,
Period_To,
case when (DATEDIFF(now(),PERIOD_TO)=75) then 1 else 0 end as ASSESSMENT_ENABLE
from
assessment;