So if a user started working in March 2002 you want to get that emplyees 20th aniversery in March 2022?
Jul 1, 2022
6
Level 1
Trying to convert my MySQL query to Query builder - Laravel
Hello, currently working on an employee management system as my internship project, I'm attempting to write a query that finds the user's work anniversary for the current month and considers a leap year as well, so here is my attempt for the MySQL query,
Edit: Sample table:
Table "*emp_detail*":
| emp_no | join_date |
| -------|------------|
| 1 | 2002-06-10 |
| 2 | 2022-06-25 |
| 3 | 2020-02-29 |
| 4 | 2002-02-15 |
| 5 | 2011-02-01 |
OUTPUT:
For FEBRUARY month current year 2022
| emp_no | join_date | anniversary_date |
| -------|------------|------------------|
| 3 | 2020-02-29 | 2022-02-28 (Here, we get 28 Feb 2022 leap year record with non leap year 2022) |
| 4 | 2002-02-15 | 2022-02-15 |
| 5 | 2011-02-01 | 2022-02-01 |
don't get any idea like how am I supposed to build a query builder through the MYSQL query
as I have split my problem into two steps:
- filtering "join_date" values using the current month
- changing the year to your "join_date"
- getting the minimum value between updated "join_date" and the last day for that date
WITH cte AS (
SELECT emp_no,
join_date,
STR_TO_DATE(CONCAT_WS('-',
YEAR (CURRENT_DATE()),
MONTH(join_date ),
DAY (join_date )),
'%Y-%m-%d') AS join_date_now
FROM tab
WHERE MONTH(join_date) = MONTH(CURRENT_DATE())
)
SELECT emp_no,
join_date,
LEAST(join_date_now, LAST_DAY(join_date_now)) AS anniversary_date
FROM cte
NOTE: Need your dev. help to build query builder
Am I going in the right direction? Any help would be great.
Please or to participate in this conversation.