결합할 수 있습니다 CASE
가 DATE_SUB
을 얻을 만료 날짜에 하위. 그런 다음 그것을 쉽게 비교하고 분석하여 각각의 경우입니다.
예를 들어:
select *,
case when expiring_date < curdate() then 'Expired'
when expiring_date > curdate() then 'Active'
else 'Expires Today'
end as status
from (
select *,
case when sub_type = 'week' then date_add(last_renewal, interval sub_duration week)
when sub_type = 'month' then date_add(last_renewal, interval sub_duration month)
when sub_type = 'year' then date_add(last_renewal, interval sub_duration year)
end as expiring_date
from subscription
) x
결과:
id sub_type sub_duration last_renewal expiring_date status
--- --------- ------------- ------------- -------------- -------
1 month 2 2021-04-12 2021-06-12 Expired
2 week 1 2021-07-11 2021-07-18 Expired
3 week 4 2021-11-11 2021-12-09 Active
보 실행하는 예제에서 DB 바이올린.