我可以连接两个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ý
..................................................................................................................