할 수 있 연결하는 두 개의 sql 란과에서 사용 DATE_ADD 기능

0

질문

내가 붙어있으로 이 문제에 대한 시간에 지금이다.

나이라는 테이블이 구독 다음과 같은 분야

  • id(int)
  • sub_type(주별,월별,년)(Varchar)
  • sub_duration(int)
  • last_renewal(날짜)

enter image description here

내가 병합하려 sub_durationsub_type 추가 last_renewal (을 얻을 만료 날짜),다음을 확인하는 경우 그 결과가 큰/작은 보다는 현재 날짜입니다. 아래는 내가 무엇을 해야 한다.

SELECT s.*
FROM subscription s
WHERE (SELECT DATE_ADD(s.last_renewal, INTERVAL (CONCAT(s.sub_duration), ' ', s.sub_type)))< CURDATE()

mysql sql
2021-11-24 03:14:18
2

최고의 응답

1

결합할 수 있습니다 CASEDATE_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 바이올린.

2021-11-24 03:52:03

감사,브로,이 수정 나의 문제이지만,나는 변경 date_subdate_add
Osah Prince

@OsahPrince 수정했습니다. 어떻게든 내가 잘못된 설명이 있습니다.
The Impaler
0

간격지 키워드 문자열이 아닌

당신이 사용할 수 있는 case when

핵심 부품의 sql 은 다음과 같이

select *
  from (
select 'week' sub_type, 1 sub_duration union all
select 'year' sub_type, 1 sub_duration) s
 where case when s.sub_type = 'week'
            then DATE_ADD(now(), INTERVAL s.sub_duration week) 
            when s.sub_type = 'month'
            then DATE_ADD(now(), INTERVAL s.sub_duration month) 
            when s.sub_type = 'year'
            then DATE_ADD(now(), INTERVAL s.sub_duration year) 
       end > now()
2021-11-24 03:28:16

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................