将”MySQL subtract bigint value as year”翻译为中文:MySQL减去bigint值作为年份。

huangapple go评论65阅读模式
英文:

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

Demo here

答案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

huangapple
  • 本文由 发表于 2023年6月8日 22:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定