英文:
Get data based on day and month from mysql date field
问题
我有一个名为employees的MySQL表,其中有一个名为'Date_of_joining'的列。我正在尝试获取入职日期最接近当前日期的员工列表。
员工:
员工编号 入职日期
1 2015-08-30
2 2019-01-15
3 2018-01-11
期望结果(假设当前日期是今天,即2023-01-09):
员工编号 入职日期
3 2018-01-11
2 2019-01-15
1 2015-08-30
我基本上正在尝试查找谁快要迎来他们的工作周年纪念日。任何线索或帮助都会感激。
编辑:
下面的查询正好按照我想要的方式工作,但我还想在两个日期之间获取类似的数据,是否可以使用相同的查询或进行任何调整来实现这一点?
SELECT *, (366 + DAYOFYEAR(Date_of_joining) - DAYOFYEAR(NOW())) % 366 as left_days FROM employees ORDER BY left_days
英文:
I have a mysql table employees with a column 'Date_of_joining'. I am trying to get the list of employees whose date of joining is closest to the current date
Employees :
EmpID Date_of_joining
1 2015-08-30
2 2019-01-15
3 2018-01-11
Expected Result (assuming the current is today ie, 2023-01-09)
EmpID Date_of_joining
3 2018-01-11
2 2019-01-15
1 2015-08-30
I am basically trying to find who is approaching their work anniversary. Any clue or help is appreciated
Edit :
The below query works exactly like I want, but i also want to get similar data between two dates, is there way to achieve this using the same query or any tweak to it
SELECT *, (366 + DAYOFYEAR(Date_of_joining ) - DAYOFYEAR(NOW())) % 366 as left_days FROM employees ORDER BY left_days
答案1
得分: 2
你想要计算并按照每一行的即将到来的周年纪念日日期进行排序。这只是简单的:
case
when Date_of_joining + interval year(current_date) - year(Date_of_joining) year < current_date then
Date_of_joining + interval year(current_date) + 1 - year(Date_of_joining) year
else
Date_of_joining + interval year(current_date) - year(Date_of_joining) year
end
(将闰年中的2月29日视为非闰年的2月28日)
英文:
You want to calculate and order by the upcoming anniversary date for each row. This is simply:
case
when Date_of_joining + interval year(current_date) - year(Date_of_joining) year < current_date then
Date_of_joining + interval year(current_date) + 1 - year(Date_of_joining) year
else
Date_of_joining + interval year(current_date) - year(Date_of_joining) year
end
(treating anniversaries of Feb 29 in a non-leap year as Feb 28)
答案2
得分: 0
两个函数会在这里有帮助。
DAYOFYEAR(d)
返回日期 d
对应年份的日数。例如,因为一月有 31 天,我们预期 DAYOFYEAR('2022-02-01')
返回 32
。
接下来,CURDATE()
返回今天的日期。
入职日期的周年纪念日始终是 DAYOFYEAR(Date_of_joining)
。所以今天和入职日期之间的天数是 DAYOFYEAR(CURDATE()) - DAYOFYEAR(Date_of_joining)
,你可以将其添加到 where
子句中。
select <choose_a_column>, DAYOFYEAR(CURDATE()) - DAYOFYEAR(Date_of_joining) as delta
from my_table
where delta > 0 # 周年纪念日已经过去
and delta < 10 # 选择你自己的数字
请注意,这是一个 SQL 查询的示例,你需要将 <choose_a_column>
替换为你希望选择的列名称。
英文:
Two functions will help here.
DAYOFYEAR(d)
returns the day number of the year for the date d
. For example, because there are 31 days in Jan, we'd expect DAYOFYEAR('2022-02-01')
to return 32
.
Next, CURDATE()
returns the date for today.
The anniversary of date of joining is always going to be DAYOFYEAR(Date_of_joining)
. So the number of days between today and date of joining is DAYOFYEAR(CURDATE()) - DAYOFYEAR(Date_of_joining)
which you can add to a where
clause.
select <choose_a_column>, DAYOFYEAR(CURDATE()) - DAYOFYEAR(Date_of_joining) as delta
from my_table
where delta > 0 # anniversary has passed
and delta < 10 # choose the number yourself
答案3
得分: 0
我知道我回答中的查询很复杂,但我还是会发布它,如果有帮助的话。<br />它不仅允许获取周年纪念日期,还允许获取周年纪念日期前的天数。
另一方面,我不得不复制获取周年纪念日期的代码,以获取天数,我不知道是否有更好的方法来做到这一点。
select *,
if(date_format(Date_of_joining,'%m%d')<date_format(now(),'%m%d'),concat((year(current_date)+1),date_format(Date_of_joining,'-%m-%d')),concat((year(current_date)),date_format(Date_of_joining,'-%m-%d'))) as 'anniversary',
DATEDIFF(if(date_format(Date_of_joining,'%m%d')<date_format(now(),'%m%d'),concat((year(current_date)+1),date_format(Date_of_joining,'-%m-%d')),concat((year(current_date)),date_format(Date_of_joining,'-%m-%d'))),NOW()) as 'nbDays'
from employees
order by anniversary
英文:
I know the query in my answer is complex but I'm posting it anyway if it helps.<br />It allows not only to have the anniversary date but also the number of days before the anniversary.
On the other hand I had to duplicate the code for obtaining the anniversary date to obtain the number of days, I don't know if there is a better way to do it.
select *,
if(date_format(Date_of_joining,'%m%d')<date_format(now(),'%m%d'),concat((year(current_date)+1),date_format(Date_of_joining,'-%m-%d')),concat((year(current_date)),date_format(Date_of_joining,'-%m-%d'))) as 'anniversary',
DATEDIFF(if(date_format(Date_of_joining,'%m%d')<date_format(now(),'%m%d'),concat((year(current_date)+1),date_format(Date_of_joining,'-%m-%d')),concat((year(current_date)),date_format(Date_of_joining,'-%m-%d'))),NOW()) as 'nbDays'
from employees
order by anniversary
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论