phpmysqlcodeignitercodeigniter-3codeigniter-query-builder

My Sql Query to get All previous records from Selected Month And year


I want to get all previous records from my mysql table from selected month and year. e.g if my selected month and year is 08-2018 then query should show all the previous records from Aug 2018.

I have tried this mysql query as :

"select * form tblusers where year(created_date)<=2018 and month(created_date)<'08'";

but it did not include record from when I have month greater than 8 e.g 09-2017 etc


Solution

  • You can try simpler way :

    Getting records created_date is lesser then 1st date of selected month and year.

    in case your created_date field is of type Date then you can try following:

    "select * form tblusers where created_date < '2018-08-01'"
    

    in case your created_date field is of type Datetime then you can try following:

    "select * form tblusers where created_date < '2018-08-01 00:00:00'"