mysql - 使用 mysql 中的间隔在数据库日期中添加月份
问题描述
我想通过加入计划表和事务表使用mysql间隔函数在事务日期中添加月份,但是这种方法不起作用但是如果我以静态方式将月份添加到事务日期它正在工作。
plan
桌子:
plan_id plan
1 6 month
2 12 month
3 3 month
transaction
桌子:
id user_id subscribed_on plan_id
1 2 2020-04-04 1
2 4 2019-02-22 2
Mysql查询(不工作):
SELECT t.* FROM transaction t inner join plan p on p.plan_id=t.plan_id
where t.user_id=2 and DATE_ADD(date(t.subscribed_on), INTERVAL p.plan) >= CURDATE()
order by t.id desc
如果我以静态方式添加月份,则它工作正常:
SELECT t.* FROM transaction t inner join plan p on p.plan_id=t.plan_id
where t.user_id=2 and DATE_ADD(date(t.subscribed_on),
INTERVAL 6 month) >= CURDATE()
order by t.id desc
解决方案
不幸的是,数据中的字符串不等于间隔。一种方法是:
date(t.subscribed_on) + interval substring_index(plan, ' ') + 0 month
注意这里month
是关键字,不是字符串。
推荐阅读
- c# - Razor Pages OnPost - 父页面是否可以使用在创建时传递给其部分的相同部分模型?
- javascript - JavsScript — 跟踪鼠标相对于屏幕中间的位置
- php - 更新到 v4.6 后,产品的 Woocommerce add_to_cart() 无法正常工作
- mysql - 如何在 GCP 上为 MySQL 启用详细指标(读取、写入、缓存)?
- r - Groupby 一列然后总结另一个值列并计算行数,然后计算 R 中每个列的百分比
- flutter - Flutter ListView.Builder Scroll 正在部分工作
- python - 如何从从文件中读取的每行字符串的开头删除 b?
- oracle - 来自 OIC 的 Twilio whatsapp REST API 调用给出此错误 400,邮递员调用工作正常
- javascript - 每当班级名称发生变化时如何发出警报
- python - Azure 函数应用:已安装依赖项,但未找到某些依赖项