Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

M@rty's avatar
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.

0 likes
6 replies
Tray2's avatar

So if a user started working in March 2002 you want to get that emplyees 20th aniversery in March 2022?

M@rty's avatar
Level 1

@Tray2 Thank you for your reply, let me edit my Question with some output, which you may find easy to understand

if the user starts working in MARCH 2002, yes definability it is the 20th anniversary for that user in MARCH 2022

here I'm getting the current month's record

From today onwards it 01 JULY, then needs those employee record whose anniversary is in JULY (current month)

Make sense? let me know if any

Tray2's avatar

@M@rty Since you ar not using the day just the month then you don't need to care about the leap years.

Something like this might work

SELECT
	name 
FROM
	emp
	 
WHERE CAST(CONCAT(YEAR(started_at), MONTH(started_at)) AS INT) - CAST(CONCAT(YEAR(NOW()), MONTH(NOW())) AS INT) = 2000;
M@rty's avatar
Level 1

@Tray2 sorry, didn't get you? could you pls explain the query I'm getting at all

Still i had attempting to run query, I don't get any record with current month

Tray2's avatar

@M@rty It converts the year and the month to a number like 202007 and then compares it to the current time 202207 and if they differ the fourth digit by 2 then it has passed 20 years.

I've updated the query since it was only counting 2 years.

M@rty's avatar
Level 1

@Tray2 I don't only need a user list that has passed 20 years, seems you are getting wrong or I'm a little bit confused :(

years = whats ever it might be (but the obvious the user has passed at least 1 year) users may join the company in the years 2001, 2004, 2021, 2022 ... so on

I need to get those records

the user has passed (at least 1 year as work anniversary day) and needs to prepare a list for the current month

like let's say an XYZ user joins on the date: 21-04-2021 then this XYZ user celebrates the 1 year anniversary with the company on the date: 21-04-2022

pls see demo

let me know if any!

Please or to participate in this conversation.