SQL select current week/last week - mysql

I'm looking for a way of selecting rows based on:
Current week (S-S)
Previous week (S-S)
The problem I'm having is selecting specifically from Sunday-Sunday.
At the moment I'm using:
SELECT SUM(time)
FROM `time`
WHERE `projectid` = '$pid' && created > DATE_SUB(NOW(), INTERVAL 1 WEEK)
Any help would be great, thanks!

MySQL does have a WEEK function that you can use:
SELECT SUM(time)
FROM `time`
WHERE
`projectid` = '$pid'
AND created > NOW() - INTERVAL 2 WEEK
AND WEEK(created) IN (WEEK(NOW()), WEEK(NOW() - INTERVAL 1 WEEK))
Notes
The first condition (created > NOW() - INTERVAL 2 WEEK) is needed to get all the data of the current and previous weeks first and then restrict for the two weeks you are interested in. Otherwise, if you had enough data, you would get the aggregation of all data of the corresponding weeks of every year in your table. It also has the added benefit of allowing the query to use an index on that field.
You also need to use "WEEK(NOW() - INTERVAL 1 WEEK)" due to the first week of the year. Otherwise, WEEK(NOW()) - 1 would have sufficed

Related

Value of time of the beginning of today

I am trying to get the number of times a user has logged in since the beginning of each day.
SELECT user_id
FROM users
WHERE TIMESTAMPDIFF(HOUR, user_login, NOW()) < 12
AND user_id = 1
This sql checks the difference between the login and the current time which is wrong.
What should go instead of NOW() to indicate midnight+1min of that day and check the logins throughout that day?
As far as I understand your code, the question is rather about mySQL and not PHP. Use
CURDATE()
instead of NOW() since TIMESTAMPDIFF will use this as 'Current Day, 00:00:00'
To get the end of the current day use
CURDATE() + INTERVAL 1 DAY
so your where-clause should be
WHERE user_login BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 DAY
provided you have the user_login field in the DATE format, otherwise you would have to cast that accordingly.

Get all `Timestamp` from 5am today

I have a column in my table that is set as curret_timestamp, so the mysql database automatically puts the timestamp in when a record is created.
I need to get all records for the current day. At the moment I have my WHERE like this:
WHERE r.ts > DATE_SUB(NOW(), INTERVAL 1 DAY)
But this is giving me the last 24hrs. Which means that in the morning it still shows yesterday. So how can I say 'Give me all r.ts from 5am this morning?'
If you want all records with a timestamp from the current day later than 5am use
SELECT * FROM t
WHERE r.ts > CONCAT(CURDATE(), ' 05:00:00')
If you want all records with a timestamps since the last time it was 5am use
SELECT * FROM t
WHERE r.ts > CONCAT(IF(CURTIME() < '05:00:00', DATE_SUB(CURDATE(), INTERVAL 1 DAY), CURDATE()), ' 05:00:00')
The queries result only in different results from 00:00:00 - 04:59:59, so the first and easier alternative should be used, if there are no queries in that time.
You can try this-
select * from t
WHERE
DATE_FORMAT(t.ts,'%Y:%m:%d') = DATE_FORMAT(now(), '%Y:%m:%d') AND
DATE_FORMAT(t.ts,'%H:%i:%s') > STR_TO_DATE('05:00:00', '%H:%i:%s');
Explaination: The first condition check for current date and match it. And than it check for time it must be greater than morning 5 AM.
SQLFiddle

mySQL - Whole of last month

I am trying to check for all records that occurred last month using the following statement.
Select * from statistics
where statistics_date
BETWEEN date_format(NOW() - INTERVAL 1 MONTH, '%Y-%m-01')
AND last_day(NOW() - INTERVAL 1 MONTH )
However, the selection does not include the last day. What I want is from the first second of the month until the last second of the month.
BETWEEN is notoriously bad for date and timestamp work because it gets the end date wrong.
Here's what you need:
First, let's compute the first day of the present month. You had that exactly right.
DATE_FORMAT(NOW(),'%Y-%m-01')
Next, let's compute the first day of last month:
DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL 1 MONTH
Now, we select the records that lie in the interval.
Select *
from statistics
where statistics_date >= DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL 1 MONTH
and statistics_date < DATE_FORMAT(NOW(),'%Y-%m-01')
Do you see how the beginning of the date range is chosen with >= and the end with <? Do you see how I used the first day of the present month for the end of the date range? Those things are important, because timestamps can have days and times in them. Consider the timestamp '2013-01-31 23:58'. It happens to be after '2012-01-31' so between won't catch it.
where MONTH(statistics_date) = MONTH(NOW()) - 1

Retrieve data whose Date between start and end date is between today and after ten day in rails or sql

I want to retrieve data whose Date between start and end date is between today and after ten day.I'm doing it in rails but even if i get the query in MySQL i can convert it to rails active record.
Something like this one :
select * from users where( between users.from and users.to = between '2012-11-25 11:52:33' and '2012-12-05 11:52:33')
The interval (a, b) overlaps with (c, d) iff a < d and b > c. Also, the curdate() function returns the current date ("today"). To calculate the date ten days into the future you can use + interval 10 day.
Combining these bits of information you get:
select ... where users.to > curdate()
and users.from < curdate() + interval 10 day
Follwing is the logic snippet not the exact code though. Please try.
select * from users
where users.fromDate between '2012-11-25 11:52:33' and '2012-12-05 11:52:33'
and users.toDate between '2012-11-25 11:52:33' and '2012-12-05 11:52:33';
Check the following sample code in * SQLFIDDLE as well
EDITING AS PER YOUR QUESTION'S REPHRASING..
Guess you are better off with,
where users.from between users.from and users.from + interval 10day
and users.to <= user.from + interval 10 day
That means, your from date can be anything (today, yesterday, one month back, two years to future...). Hence your to-date will be validated against any date that is 10 days interval from the above from-date
Hope it makes sense... but again, Joni's answers fills for your today's consition. :)

Trying to select the number of mysql rows inserted yesterday

I can normally do this but it appears my brain is not functioning well right now and I'm missing something.
Every day via a cron job that is run at 1am, I want to get a count of rows that were inserted yesterday, and the date.
ie
SELECT DATE(added) as date, COUNT(*) FROM bookings WHERE added = DATE_SUB(NOW(), INTERVAL 1 DAY) GROUP BY date
'added' contains a timestamp ie '2011-04-18 12:31:31'
What am I getting wrong here? I know there are many rows added yesterday but my query is returning 0 results and no mysql_errors :(
Any ideas?
Please try
SELECT DATE(added) as yesterday, COUNT(*) FROM bookings WHERE DATE(added) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY yesterday
or perhaps
SELECT DATE(added) as yesterday, COUNT(*) FROM bookings WHERE DATE(added) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) GROUP BY yesterday
Updated
Corrected the WHERE part.
well whatever NOW() is will return the time portion and unless they were added at exactly that time the day before they wont be counted.
So either use BETWEEN and specify time range, or format the date in your query to only match on the day month year components and not time
WHERE added = does only match exact NOW() - 1 DAY, you should select by a range instead.