将 MySQL 中的整列从 YYYY-MM-DD 更改为月数

发布于 2025-01-21 00:21:43 字数 874 浏览 0 评论 0 原文

假设您在MySQL中有一个Yyyy-MM-DD中的整列,并且您想选择它作为到特定日期的月数,也将其作为新列进行。示例日期计数可以是2020年3月。

因此( table_1 ):

emp_name hire_date
steve steve 2018-03-28

to

emp_name monter_employs_employs_employed
steve 24,

但在hire_date列中的每个日期都执行此操作。在日期或时间戳记的开始日期和结束日期。

我有以下内容,它返回了一个新列,但是以空值为单位:

select
Emp_Name, timestampdiff(month, 2021-04-01, Hire_Date) as Months_Employed
from
table_1

我还尝试了约会夫,并除以12,但行不通。

任何帮助都将受到赞赏。

编辑为有效的答案:

SELECT 
emp_name, 
TIMESTAMPDIFF(month, hire_date, '2022-03-07')
AS months_employed
FROM table_1

Suppose you have an entire column in MySQL that is YYYY-MM-DD, and you want to select it as the number of Months to a specific date, and also do it as a new column. Example date counting to can be March 2020.

So the following (table_1):

Emp_Name Hire_Date
Steve 2018-03-28

To

Emp_Name Months_Employed
Steve 24

But do this for every date in the Hire_Date column without manually entering the start and end date in DATEDIFF or TIMESTAMPDIFF.

I have the following, which returns a new column, but with NULL values:

select
Emp_Name, timestampdiff(month, 2021-04-01, Hire_Date) as Months_Employed
from
table_1

I have also tried DATEDIFF and dividing by 12 but it doesn't work.

Any help is appreciated.

EDIT for Answer that worked:

SELECT 
emp_name, 
TIMESTAMPDIFF(month, hire_date, '2022-03-07')
AS months_employed
FROM table_1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微暖i 2025-01-28 00:21:43

是使用的正确功能。

日期常数需要显示为文本字符串。 '2021-04-01'是愚人节2021。代码>。

这是 fiddle

SELECT emp_name, 
       hire_date, 
       TIMESTAMPDIFF(MONTH, hire_date, '2022-03-07') 
  FROM table_1

TIMESTAMPDIFF(MONTH, startDate, endDate) is the correct function to use.

Date constants need to be shown as text strings. '2021-04-01' is April Fools Day 2021. But 2021-04-01, without the quotes, is an arithmetic expression evaluating to 2016.

Here's a fiddle.

SELECT emp_name, 
       hire_date, 
       TIMESTAMPDIFF(MONTH, hire_date, '2022-03-07') 
  FROM table_1
驱逐舰岛风号 2025-01-28 00:21:43

有一个 mysql 函数可以让你通过传递年份和月份来实现这一点。
PERIOD_DIFF(P1,P2) - 返回期间 P1 和 P2 之间的月数。 P1 和 P2 的格式应为 YYMM 或 YYYYMM。请注意,句点参数 P1 和 P2 不是日期值。

SELECT PERIOD_DIFF(200802,200703);
    -> 11

SELECT emp_name,
   PERIOD_DIFF
   (
     202104,
     DATE_FORMAT(hire_date,"%Y%m")
   ) AS "Months_Employed"
FROM employees;

There is a mysql funtion that lets you achieve this by passing in the year and month.
PERIOD_DIFF(P1,P2) - Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM or YYYYMM. Note that the period arguments P1 and P2 are not date values.

SELECT PERIOD_DIFF(200802,200703);
    -> 11

SELECT emp_name,
   PERIOD_DIFF
   (
     202104,
     DATE_FORMAT(hire_date,"%Y%m")
   ) AS "Months_Employed"
FROM employees;
压抑⊿情绪 2025-01-28 00:21:43

您可以使用此查询中的功能添加生成的列:

select d,timestampdiff(month,'2020-03-01',d) X from dates where day(d)=1 and year(d)<=2020;

输出:

D X
2020-01-01 -2
2020-02-02-01 -1
2020-03-01 0
2020-04-01 1
2020-05-01 2
2020-06-01 3
... ...

在您的情况下,类似:

alter table table_1 
modify column Months_Employed integer 
   generated always as (timestampdiff(month,'2020-03-01',Hire_Date)) stored;

Alter表和生成的列

You could add a generated column using the functionality like in this query:

select d,timestampdiff(month,'2020-03-01',d) X from dates where day(d)=1 and year(d)<=2020;

output:

d X
2020-01-01 -2
2020-02-01 -1
2020-03-01 0
2020-04-01 1
2020-05-01 2
2020-06-01 3
... ...

In your case something like:

alter table table_1 
modify column Months_Employed integer 
   generated always as (timestampdiff(month,'2020-03-01',Hire_Date)) stored;

More info: ALTER TABLE and Generated Columns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文