I am trying to get the total amount of registered users per day. At the moment I am using this:
$sql = "SELECT name, email FROM users WHERE DATE_SUB(NOW(), INTERVAL 1 DAY) < lastModified"
But I am not sure if this works per day or per 24 hours?
For example, a user who registered 22 hours ago shouldn't be returned. I just want the user of today(=Tuesday).
lastModified is, presumably, a datetime. To convert this into a date you can simply wrap it in DATE() i.e. DATE(lastModified). DATE() returns the date part of a datetime value which is effectively 00:00 on that day.
SELECT
name,
email
FROM users
WHERE DATE(lastModified) = DATE( DATE_SUB( NOW() , INTERVAL 1 DAY ) )
Using this to match a WHERE though would be inefficient as all rows would require DATE applied to them and so it would probably scan the whole table. It is more efficient to compare lastModified to the upper and lower bounds you are looking for, in this case >= 00:00 on SUBDATE(NOW(),INTERVAL 1 DAY) and < 00:00 on NOW()
Therefore you can use BETWEEN to make your select giving the following.
SELECT
name,
email
FROM users
WHERE lastModified
BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 1 DAY ) )
AND DATE ( NOW() )
I think you need
SELECT
name,
email
FROM users
WHERE DATE(lastModified) = DATE( NOW() )
This effectively "rounds to the date only" and will therefore only match records "since midnight".
Related
i want my code to retrieve rows that will be due in the less than 2 weeks so that i can send a reminder to the user to renew there subscription
$Q = "SELECT * FROM records WHERE dateDue BETWEEN (dateDue - INTERVAL 2 WEEK) AND NOW()";
That would be:
select *
from records
where dateDue > now() and dateDue <= now() + interval 2 week
Or, if your date has not time component (that is, you have a date, not a timestamp):
select *
from records
where dateDue > current_date and dateDue <= current_date + interval 2 week
Your question does not mentions whether you want inclusive or exclusive intervals. You might need to adjust the inequalities accordingly.
Hi all, I have pretty awfull query, that needs optimizing.
I need to select all records where date of created matches NOW - 35days, but the minutes and seconds can be any.
So I have this query here, its ugly, but working:
Any optimisation tips are welcome!
SELECT * FROM outbound_email
oe
INNER JOIN (SELECT `issue_id` FROM `issues` WHERE 1 ORDER BY year DESC, NUM DESC LIMIT 0,5) as issues
ON oe.issue_id = issues.issue_id
WHERE
year(created) = year( DATE_SUB(NOW(), INTERVAL 35 DAY) ) AND
month(created) = month( DATE_SUB(NOW(), INTERVAL 35 DAY) ) AND
day(created) = day( DATE_SUB(NOW(), INTERVAL 35 DAY) ) AND
hour(created) = hour( DATE_SUB(NOW(), INTERVAL 35 DAY) )
AND campaign_id IN (SELECT id FROM campaigns WHERE initial = 1)
I assume the field "created" is a datetime field and is from the issues table? Since you don't need anything else on the issues and campaign table, then you can do the following:
SELECT e.* FROM outbound_email e
JOIN issues i ON e.issue_id = i.issue_id
JOIN campaigns c ON c.id = i.campaign_id
WHERE i.created < DATE_SUB(NOW(), INTERVAL 35 DAY)
AND c.initial = 1
There's no need to separate the datetime field into years, months...etc.
You seem to be saying you want to select all rows from a table where the time they were created was the same hour as it is currently, 35 days ago
SELECT * FROM table WHERE created BETWEEN
DATE_ADD(CURDATE(), INTERVAL (HOUR(now()) - 840) HOUR) AND
DATE_ADD(CURDATE(), INTERVAL (HOUR(now()) - 839) HOUR)
Why does it work? Curdate gives us today at midnight. We add to this the current hour of the time (e.g. Suppose it's now 5pm we'd add `HOUR(NOW()) which would give us 17, for a time now of 5pm) but we also subtract 840 because that's 35 days * 24 hours a day = 840 hours. Date add will hence add -823 hours to the current date, i.e. 5pm 35 days ago
We make the search a range to get all the records from the hour, the simplest way to specify an hour later is to subtract 839 hours instead of 840
Technically this query will also return records that are bang on 6pm (but not a second later) 35 days ago too because between is inclusive (between 1 and 10 will return 10 also
If this is a problem, change the BETWEEN for created >= blah AND created < blahblah
I haven't put the rest of your query in for reasons of clarity
As a side note, the way you did it wasn't bad- you could have simplified things by not having the year/month/day parts, just dropping the time part of the date with date(created) = date_sub(curdate(), interval 35 day) which is the year month and day combined as a date, no time element.. BUT it is generally always best to leave table data alone rather than format or convert it just to match a query. If you convert table data then indexes can no longer be used. If you go the extra mile to get your query parameters into the format of the column, and don't convert the table data then indexes on the column can be used
How can i display a range of date from my Date field in the database into a table?
I have a field called: Date from the database that Display the event date of 5 days. And I would like to display only 1 day and 12 hours (36 hours) using SQL Statement.
Field name : date, details from Events_new table. This table display 5 days of date and time records. and It's updates automatically. My point is that I need a SQL Query that will fetch the last 36 hours from the table
I tried many time. This is my SQl Statement but I can't get it right.
SELECT
details , datediff(date , interval 36 hour) as substratehours
FROM
events_new
WHERE
Stuff_id = 4932
order by date ASC
And This :
SELECT
date,
details
FROM
events_new
WHERE
stuff_id = 4932
AND date = (date - interval 36 hour)
And This:
SELECT
date,
details
FROM
events_new
WHERE
stuff_id = 4932
AND date > date_sub(date,interval 36 hour)
order by date ASC
I Still Don't get it right! Anyone with a subjection?
Thank you
To get the rows having date in the most recent 36 hours only:
SELECT details
FROM events_new
WHERE Stuff_id = 4932
AND `date` > date_sub(NOW(), INTERVAL 36 HOUR)
ORDER BY `date` ASC
If you don't want the 36 hours interval to end now but at some arbitrary moment in time then you should use BETWEEN to compare the value of field date with the interval bounds:
SELECT details
FROM events_new
WHERE Stuff_id = 4932
AND `date` BETWEEN date_sub('2015-01-19 12:00:00', INTERVAL 36 HOUR)
AND '2015-01-19 12:00:00'
ORDER BY `date` ASC
Replace '2015-01-19 12:00:00' with the moment when you want your 36 hours interval ends.
Take a look at MySQL documentation about the SELECT statement and date & time functions.
In mysql
SELECT DATE_ADD(date,INTERVAL -36 hour) AS date from events_new
In mssql
select DATEADD(hour , -36 , date )AS date from events_new
I'm trying to find a subset of users who joined a site within the last week
The table is users and date field (dateadded) is unix timestamp e.g. 2012-04-29 17:31:57
Here's what I'm trying but its returning all users:
SELECT * FROM users
WHERE dateadded <= NOW() AND dateadded >= DATE_SUB(dateadded, INTERVAL 7 DAY)
You have the wrong condition. You want now() for both comparisons:
SELECT *
FROM users
WHERE dateadded <= NOW() AND dateadded >= DATE_SUB(now(), INTERVAL 7 DAY)
I always have trouble with complicated SQL queries.
This is what I have
$query = '
SELECT id,
name,
info,
date_time
FROM acms_events
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
AND active = 1
ORDER BY date_time ASC
LIMIT 6
';
I want to get up to 6 rows that are upcoming within the hour. Is my query wrong? It does not seem to get events that are upcoming within the next hour when I test it.
What is the correct syntax for this?
I'm going to postulate that you're looking at a group of records that contain a range of DATETIME values, so you probably want something more like this:
SELECT id,
name,
info,
date_time
FROM acms_events
WHERE date_time < DATE_ADD(NOW(), INTERVAL 1 HOUR)
AND date_time >= NOW()
AND active = 1
ORDER BY date_time ASC
LIMIT 6
Otherwise, your query is looking for records with a date_time of exactly "now + 1 hour". I'm assuming all your dates aren't specific to that particular second. ;)
To clarify a bit, DATE_ADD() and DATE_SUB() return exact timestamps, so your query above roughly translates to something like SELECT ... WHERE date_time = '2010-04-14 23:10:05' ORDER BY ..., which I don't think is what you want.
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
means date_time equals exactly now minus one hour, which would result in any record exactly one hour old.
Why not use
WHERE TIMEDIFF(date_time, NOW()) < '01:00:00'
AND date_time > NOW()