Posso concatenare due colonna sql e utilizzare in funzione DATE_ADD

0

Domanda

Ho attaccato con questo problema per ore ora.

Ho una tabella chiamata sottoscrivere con i seguenti campi

  • id (int)
  • sub_type (settimana, mese, anno) (Varchar)
  • sub_duration (int)
  • last_renewal (Data)

enter image description here

Voglio unire la sub_duration e sub_type e aggiungere il last_renewal (per ottenere la data di scadenza), Quindi verificare se il risultato è maggiore/minore rispetto alla data corrente. Di seguito è quello che ho fatto.

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

Migliore risposta

1

È possibile combinare CASE con DATE_SUB per ottenere la data di scadenza in una sottoquery. Allora è facile confrontare e analizzare ogni singolo caso.

Per esempio:

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

Risultato:

 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  

Vedere esempio di esecuzione al DB Violino.

2021-11-24 03:52:03

Grazie, fratello, questo risolto il mio problema, ma ho cambiato il tuo date_sub per date_add
Osah Prince

@OsahPrince Fisso. In qualche modo ho letto male la spiegazione.
The Impaler
0

INTERVALLO accetta parole chiave, non stringhe

è possibile utilizzare case when

La parte fondamentale di sql è come questo

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

In altre lingue

Questa pagina è in altre lingue

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