Manoj Dhiman
Manoj Dhiman

Reputation: 5166

Get last 3 months record from paid salary table where month and year is stored in separate column

I need help with the MySQL query. I have a table like this in MySQL

What I want is to get the last 3 months' records from the table. But I don't understand how to make this MySQL query.

I have tried to contact both columns and tried to generate a MySQL date to compare but it returns me null.

I also tried this query and it works for current data. But i am not sure if it is the only way to do this or is there any better way.

Upvotes: 0

Views: 545

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269703

What I want is to get the last 3 months' records from the table.

I think you want a where clause like this:

date(concat_ws('-', salary_year, salary_month, 1)) >= (curdate() - interval (1 - day(curdate()) day) - interval 2 month

The key idea here is to convert the string to a date so it is handled correctly.

If you have a fixed date, then you would represent that as:

date(concat_ws('-', salary_year, salary_month, 1)) >= '2021-04--01'

Upvotes: 2

Related Questions