英文:
MySQL subtract bigint value as year
问题
我有一个MySQL数据库,其中包含一个名为years_valid_for的bigint列和一个名为completion_date的datetime(6)列。我需要从bigint列中减去年份。
例如,years_valid_for为4,completion_date为2023-06-07。我需要从2023-06-07减去4年,得到2019-06-07。
是否可能?如果可能,该如何实现?
我尝试使用DATE_SUB函数,但由于语法错误,未能实现预期的结果。
英文:
I have a MySQL database which holds one column years_valid_for as bigint and another column completion_date as datetime(6). I have to subtract bigint columns number as years.
e.g. years_valid_for is 4 and completion_date is 2023-06-07. I have to subtract 4 years from 2023-06-07 and get 2019-06-07.
Is it possible? If so, how?
I have tried DATE_SUB function but could not achieve the expected result due to syntax errors
答案1
得分: 0
以下是翻译好的部分:
你可以按以下方式操作:
SELECT *, completion_date - INTERVAL years_valid_for YEAR
FROM mytable
英文:
You can do it as follows :
SELECT *, completion_date - INTERVAL years_valid_for YEAR
FROM mytable
答案2
得分: 0
Syntax:- DATE_SUB(date, INTERVAL value interval)
SELECT DATE_SUB(completion_date, INTERVAL years_valid_for YEAR) AS res_date FROM your_table_name
中文翻译:
语法:DATE_SUB(日期, 间隔 值 单位)
SELECT DATE_SUB(completion_date, INTERVAL years_valid_for YEAR) AS res_date FROM your_table_name
英文:
Syntax:- DATE_SUB(date, INTERVAL value interval)
SELECT DATE_SUB(completion_date, INTERVAL years_valid_for YEAR) AS res_date FROM your_table_name
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论