Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select two weeks ago in MYSQL?

Tags:

sql

mysql

I have a report that is driven by a sql query that looks like this:

   SELECT batch_log.userid,
         batches.operation_id,
         SUM(TIME_TO_SEC(ramses.batch_log.time_elapsed)),
         SUM(ramses.tasks.estimated_nonrecurring + ramses.tasks.estimated_recurring),
         DATE(start_time)
    FROM batch_log
    JOIN batches ON batch_log.batch_id=batches.id
    JOIN ramses.tasks ON ramses.batch_log.batch_id=ramses.tasks.batch_id
    JOIN protocase.tblusers on ramses.batch_log.userid = protocase.tblusers.userid
   WHERE DATE(ramses.batch_log.start_time) > "2011-02-01" 
     AND ramses.batch_log.time_elapsed > "00:03:00" 
     AND DATE(ramses.batch_log.start_time) < now() 
     AND protocase.tblusers.active = 1
     AND protocase.tblusers.userid NOT in ("ksnow","smanning", "dstapleton")
GROUP BY userid, batches.operation_id, date(start_time)
ORDER BY start_time, userid ASC

Since this is to be compared with the time from the current payperiod it causes an error.
Our pay periods start on a Sunday, the first pay period was 2011-02-01 and our last pay period started the 4th of this month. How do I put that into my where statement to strip the most recent pay period out of the query?

EDIT: So now I'm using date_sub(now(), INTERVAL 2 WEEK) but I really need a particular day of the week(SUNDAY) since it is wednesday it's chopping it off at wednesday.

like image 586
davidahines Avatar asked Jul 13 '11 18:07

davidahines


People also ask

How do I query between two dates using MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.

How do I select a specific date in MySQL?

You can use DATE() from MySQL to select records with a particular date. The syntax is as follows. SELECT *from yourTableName WHERE DATE(yourDateColumnName)='anyDate'; To understand the above syntax, let us first create a table.

How do I get last week records in MySQL?

Here's the SQL query to get last week's data in MySQL. In the above SQL query, we use WEEK() function to get Week number of order_date column. We select only those records whose week number is 1 less than the week number of today's date, obtained used NOW() function.

How do I get last 7 days record in SQL?

We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.


1 Answers

You want to use DATE_SUB, and as an example.

Specifically:

select DATE_SUB(curdate(), INTERVAL 2 WEEK)

gets you two weeks ago. Insert the DATE_SUB ... part into your sql and you're good to go.

Edit per your comment:

Check out DAYOFWEEK:

and you can do something along the lines of:

DATE_SUB(DATE_SUB(curdate(), INTERVAL 2 WEEK), INTERVAL 2 + DAYOFWEEK(curdate()) DAY)

(I don't have a MySql instance to test it on .. but essentially subtract the number of days after Monday.)

like image 113
Reverend Gonzo Avatar answered Nov 02 '22 00:11

Reverend Gonzo