MySQL where date is between two timestamps - mysql

I try to select all date between to dates/times. Unfortunately i get no resolute. I have seen a lot of examples where people try to select data between ex. '2012-08-27' without the time. I need the time as well.
Here is my query. Can anyone tell me whats wrong? The row date has the timestamp as datatype.
SELECT date, points
FROM nf_publicpoints
WHERE date BETWEEN '2012-08-27 00:00:00' AND '2012-08-02 00:00:00'
ORDER BY points DESC
LIMIT 10
Thanks in advance
Troels

try using DATE function
WHERE DATE(date) BETWEEN '2012-08-02' AND '2012-08-27'
if you need time,
WHERE date BETWEEN '2012-08-02 00:00:00' AND '2012-08-27 23:59:59'

there is no syntax issue on that query.
I think you simply have to turn around your query like so:
SELECT date, points
FROM nf_publicpoints
#canged that || ||
WHERE date BETWEEN '2012-08-02 00:00:00' AND '2012-08-27 00:00:00'
ORDER BY points DESC
LIMIT 10
The time has to be ginven in increasing order, as there is no time between 27 and 02 of August, but there is one between 02 and 27.
Hope this helps

Related

DateTime condition [duplicate]

I get a datetime field, that's currently in the query as:
SELECT DATE_FORMAT(x.date_entered, '%Y-%m-%d') AS date FROM x ORDER BY date ASC
What I want to do is to subtract 3 hours from that date (GMT issues), but I can't do it in PHP as PHP only knows the date part, not the time.
mySQL has DATE_SUB():
SELECT DATE_SUB(column, INTERVAL 3 HOUR)....
but would it not be better to try and sort out the underlying time zone issue instead?
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'),
'%Y-%m-%d') AS date
FROM x ORDER BY date ASC;
Normal select query.
Once applied DATE_ADD() function in MySQL
select lastname,
date_add(changedat, interval -24 hour) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.

Get 7 day average for timeframe?

I'm currently running the following query to get the daily average of entries per user on my database, it's working as expected but I want to modify it to get the 7 day averages by day.
SELECT
AVG(bg),
AVG(carbs),
timestamp
FROM users_entries
WHERE uid = '10b47fded7d2ea8d' AND
timestamp >= '2019-01-01 00:00:00' AND timestamp <= '2019-01-30 00:00:00'
GROUP BY DAY(timestamp)
So for example, for the time frame, say 2019-01-01 00:00:00 to 2019-06-01 00:00:00 I would like to find all averages for 7 days and list them out. Basically take each day in the time frame, go back 7 days and get the average of the columns I select.
I'm thinking that this would require some sort of subquery but based on what I see online I do not understand them well enough to figure it out on my own, any help would be great.
In MySQL 8+, you can use window functions:
SELECT DATE(timestamp),
AVG(bg),
AVG(carbs),
AVG(AVG(bg)) OVER (ORDER BY DATE(timestamp) ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) as bg_7,
AVG(AVG(carbs)) OVER (ORDER BY DATE(timestamp) ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) as bg_7,
FROM users_entries
WHERE uid = '10b47fded7d2ea8d' AND
timestamp >= '2019-01-01' AND
timestamp < '2019-01-30'
GROUP BY DATE(timestamp);
This is much more challenging in older versions of MySQL.

Return results for current date with any time

I have a simple query that means to count all OId's from a table for the current day. I want to put it in to Crystal Reports so users can generate a report daily that counts all results for the day they run it.
The code as i first figured would be:
SELECT COUNT(oid) from TABLE_A where DATE = CURDATE()
But CURDATE() only returns results with 00:00:00 time stamp. So i get all results with 2015-08-06 00:00:00 as the date.
How do i get all results for the day regardless of time stamp?
Thanks in advance!
Mickey!
You can write your query in a sargable way as
SELECT COUNT(oid)
from TABLE_A
where DATE >= concat(CURDATE(),' 00:00:00')
and DATE <= concat(CURDATE(),' 23:59:59')
For Current Date & Time
$CurrentDateTime=date("d-m-Y h:i:s");
=> 06-08-2015 15:33:21
For Current Date
$CurrentDate=date("d-m-Y");
=> 06-08-2015

Select between date range and specific time range

Is there a way to Select records between dates and specific time range. For example all the records from 2013-11-01 to 2013-11-30 between hours 05:00 to 15:00. Here what i made until now.
select count(*) as total from tradingaccounts accounts
inner join tradingaccounts_audit audit on audit.parent_id = accounts.id
where date(audit.date_created) between date('2013-11-01 00:00:00') AND date('2013-11-01 23:59:59')
But how am I going to set the specific time range?
You can use the HOUR function to add an additional condition on the hours:
select count(*) as total from tradingaccounts accounts
inner join tradingaccounts_audit audit on audit.parent_id = accounts.id
where date(audit.date_created) between date('2013-11-01 00:00:00') AND date('2013-11-01 23:59:59')
AND HOUR (audit.date_created) BETWEEN 5 AND 15
As others answered, HOUR() could help you.
But HOUR() or DATE() cannot use INDEX. To make query faster, I suggest that add time_created TIME column and save only TIME part. after that ADD INDEX(date_created, time_created). finally with below query, you can retrieve rows with high speed.
where audit.date_created between '2013-11-01 00:00:00' AND '2013-11-01 23:59:59'
AND audit.time_created BETWEEN '05:00:00' AND '15:00:00'
Replace your DATE function, it skips time part. Use TIMESTAMP instead.
add following line in query and set your hour in BETWEEN
and time(audit.date_created) between '00:00:01' AND '01:00:00'
If you use date(audit.date_created), the index on date_created field could not take effect.
Just simply use where audit.date_created >= 'xx-xx-xx' and audit.date_created < 'xx-xx-xx'

Select mysql query between date?

How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??
My column "datetime" is in datetime date type. Please help, thanks
Edit:
If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?
select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()
or using >= and <= :
select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()
All the above works, and here is another way if you just want to number of days/time back rather a entering date
select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
You can use now() like:
Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();
Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()), make sure to specify the hours, minutes and seconds (2019-12-02 23:59:59) on the end date or you won't get any results from that day, i.e.:
This WILL include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'
This WON'T include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'