Posso concatenar dois coluna sql e usá-lo em função DATE_ADD

0

Pergunta

Eu ter sido preso com esse problema por horas agora.

Eu tenho uma tabela chamada assinar com os seguintes campos

  • id (int)
  • sub_type (semana, mês, ano) (Varchar)
  • sub_duration (int)
  • last_renewal (Data)

enter image description here

Eu quero mesclar o sub_duration e sub_type e adicioná-lo para o last_renewal (para obter a sua data de expiração), em Seguida, verifique se o resultado é maior/menor do que a data atual. Abaixo está o que eu tenho feito.

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

Melhor resposta

1

Você pode combinar CASE com DATE_SUB para obter a data de expiração de uma subconsulta. Em seguida, é fácil comparar e analisar cada caso.

Por exemplo:

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

Resultado:

 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  

Ver exemplo em execução em DB Violino.

2021-11-24 03:52:03

Obrigado, mano, isso seja resolvido o meu problema, mas eu mudei a sua date_sub para date_add
Osah Prince

@OsahPrince Fixo. De alguma forma eu não leram a explicação.
The Impaler
0

INTERVALO aceita palavras-chave, não com cadeias de caracteres

você pode usar case when

A parte chave do sql é como este

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

Em outros idiomas

Esta página está em outros idiomas

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