Ankit Pundir
Ankit Pundir

Reputation: 29

how to select row in mysql on basis of 3 different column

I have a table consisting reports, I want to select data of last 7 day's. So in below image you can see 3 column day's month year.

What is the sql query to select last 7 day's.

enter image description here

Upvotes: 0

Views: 74

Answers (2)

Hytool
Hytool

Reputation: 1368

try something like this,

    SELECT 
            *    
    FROM 
            < your_table > 
    WHERE 
            STR_TO_DATE(CONCAT(`day`, ',', `month`, ',' ,`year`), '%e,%m,%y') 
            BETWEEN 
            DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE(); 

If you want to use latest date from the same table,

try fetch max date separately with query ,

SELECT MAX(STR_TO_DATE(CONCAT(`day`, ',', `month`, ',' ,`year`), '%e,%m,%y')) AS maxdat FROM < your_table > 

store it, and use it in the main query in-place of CURDATE(),

or,

you can use the same query as a sub-query if you don't have any performance issues.

Upvotes: 1

ojus kulkarni
ojus kulkarni

Reputation: 1907

On which basis you want last 7 days entry.

Try this to find date before 7 days:

WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -7 DAY);

OR

WHERE t.date >= DATE(NOW()) - INTERVAL 7 DAY

Or you can go through Date_doc documentation.

Upvotes: 0

Related Questions