I'm trying to get all records which have a date_created within 2 hours ago. It's a unix timestamp which is created from the php function time(). Here is my current query:
SELECT id from gsapi_synsets where name = "Beyonce" and date_created BETWEEN UNIX_TIMESTAMP(date_created) and UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 2 hour))
Doesn't seem to be working though.
If none of the date_created values will be in the future, you can use this:
SELECT id
FROM gsapi_synsets
WHERE name = 'Beyonce'
AND date_created > UNIX_TIMESTAMP(NOW() - INTERVAL 2 HOUR);
If date_created values can be in the future, use this, which cuts off at the current
SELECT id
FROM gsapi_synsets
WHERE name = 'Beyonce'
AND date_created BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 2 HOUR) AND UNIX_TIMESTAMP()
Note that calling UNIX_TIMESTAMP without an argument returns the timestamp for "right now".
You're trying to get all row's between that row's datestamp and 2 hours from now, which won't work. Use this instead:
SELECT id from gsapi_synsets where name = "Beyonce" and date_created BETWEEN UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL -2 hour)) and UNIX_TIMESTAMP(NOW())
Related
I want to retrieve records from db according to date format YYYY,MM,dd given by me but the column type is YYYY,MM,dd hh:mm:ss.
tried to use Date format function
SELECT *
FROM tabl.error_logs
where created_at = DATE_FORMAT(NOW(),'%Y-%m-%d'- INTERVAL 3 DAY);
I expect the created date will be 2019-06-08, but the result is empty
What is the actual datatype of created_at column?
This answer is going to ignore that funkiness with the format with commas, and assume that it's not character type data, and that it's DATETIME or TIMESTAMP.
Normative pattern for predicates on DATETIME and TIMESTAMP columns is a range comparison.
For example, to get all datetimecol values on June 10th, then something like this:
WHERE t.datetimecol >= '2019-06-10 00:00:00'
AND t.datetimecol < '2019-06-11 00:00:00'
Typically, I would just pass that one date value, and let MySQL figure out the next day. If we omit the time portion, MySQL will assume 00:00:00
WHERE t.datetimecol >= '2019-06-10' + INTERVAL 0 DAY
AND t.datetimecol < '2019-06-10' + INTERVAL 1 DAY
For performance, to allow MySQL to make effective use of a range scan operation on a suitable index, we want to avoid wrapping the column reference in a function. That is, specifying DATE(t.datetimecol) in a condition in the WHERE clause is going to force MySQL to evaluate the DATE() function on every row in the table.
With references to the bare column, that allows MySQL to make use of an index, if a suitable index is available.
e.g.
SELECT e.*
FROM tabl.error_logs e
WHERE e.created_at >= DATE(NOW()) + INTERVAL -3 DAY
AND e.created_at < DATE(NOW()) + INTERVAL -2 DAY
note that we can easily test those expressions in the WHERE clause, to verify they are returning what we want, and tweak as necessary:
SELECT DATE(NOW()) + INTERVAL -3 DAY
, DATE(NOW()) + INTERVAL -2 DAY
To make your query sargable, you need ...
SELECT *
FROM tabl.error_logs
WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 3 DAY)
AND created_at < DATE_SUB(CURDATE(), INTERVAL 2 DAY)
This selects all values of created_at on or after midnight three days ago, up to but not including < midnight two days ago. It uses a range scan on an index on created_at if one is available.
You coudl use date_sub()
SELECT *
FROM tabl.error_logs
where date(created_at) = DATE_SUB(date(now()), INTERVAL 3 DAY);
if the column created_at is a date then you could avoid the date() function and let the index (if present) work for this column
SELECT *
FROM tabl.error_logs
where created_at = DATE_SUB(date(now()), INTERVAL 3 DAY);
I am trying to create a query that will limit insertion into a table based on the last time the poster sent data to the table.
For example if you posted data to the table then you are locked out of the system for another 10 hours. Here is what I came up with so far. But I get nowhere with the actual results on the data. Any help?
SELECT DATE( `date` )
FROM tablename
WHERE DATE( CURDATE( ) ) < CURDATE( ) - INTERVAL 1002
DAY
LIMIT 0 , 30
This will return a single post from the last 10 hours, if it exists:
SELECT *
FROM tablename
WHERE `date` >= NOW() - INTERVAL 10 HOUR
LIMIT 1
I'm assuming date is declared as DATETIME, since actual DATE does not contain the time part and hence is only day-accurate.
If date is an integer UNIX timestamp, use this:
SELECT *
FROM tablename
WHERE `date` >= UNIX_TIMESTAMP(NOW() - INTERVAL 10 HOUR)
LIMIT 1
There are a number of ways you could do this. Perhaps if you have a user settings table you could simply add a "last_insert" field, and store the timestamp as an integer value- that would be a super simple way to do it- you could check the current timestamp vs user_settings.last_insert and voila!
I suppose you could use datetime too. Whatever floats the boat.
First of all, you need a DATETIME column and not a DATE column. Assuming that tablename.date is a DATETIME column, then 10 hours before right now is CURRENT_TIMESTAMP - INTERVAL 10 HOUR.
First of all create a Time (TIMESTAMP DEFAULT CURRENT_TIMESTAMP) columnt in your table. It will be automatically set to current date on row insert
Then check:
SELECT COUNT(*) FROM Table WHERE Time > NOW() - INTERVAL 10 HOUR
If its 1 or more - block
You must compare the time last post was put with current time, not current time with current time :|
I am trying to fetch previous day records from table but I am not finding how to do it exactly. Need your help please..
Table: RECORD_DATA
id creationDate
1 | 2013-05-03 04:03:35 |
2 | 2013-05-03 04:03:35 |
Now I need to get all the records that were created on 2013-05-03. Time can be anything. So my query should have LIKE operator.
I am using below query and it gives me empty set.
select creationDate from RECORD_DATA
where creationDate LIKE DATE_SUB(STR_TO_DATE('2012-04-05','%d/%m/%Y'),INTERVAL 1 DAY);
Fairly simple when done with SQL, just add the condition
WHERE creationDate BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()
There is no need to convert creationDate as it is already a date :). And i belive that this will be the fastest way to check it (which will matter if you go over large data sets).
The answers here seem to be answering 'How do I find something within the past 24 hours?', not 'How do I find something from the previous calendar date?'. Different questions.
If you need to find something from the previous calendar date, you can try:
select creationDate from RECORD_DATA
where date(creationDate) = curdate() - INTERVAL 1 DAY;
No need to convert dates to strings and do string comparisons.
This will use INDEX on creationDate (if there is any).
SELECT *
FROM TableName
WHERE creationDate >= CURDATE() - INTERVAL 1 DAY AND
creationDate < CURDATE()
I think this should do it:
SELECT * FROM RECORD_DATA WHERE `creationDate` >= CURDATE()-1 and `creationDate` < CURDATE();
You can use
SELECT *
FROM `tableName`
WHERE DAY(created) = DAY(CURRENT_DATE - INTERVAL 1 DAY)
I have all dates stored as timestamps (int) in the database.
how can I get dates that are exactly 3 days earlier?
I tried
SELECT date from user WHERE DATE_ADD(DATE( FROM_UNIXTIME( `created` ) ), INTERVAL 3 DAY) = CURDATE()
is that the best/most efficient way to do it?
i think the database prefer to only do the date add 2 times to define the range, like:
SELECT date FROM user
WHERE UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -3 DAY)) <= `created`
AND `created` < UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -2 DAY));
Test putting DESCRIBE keyword before SELECT in both case, and the database respond with how its going to perform the query
SELECT date
FROM user
WHERE created = UNIX_TIMESTAMP(CURRENT_DATE - INTERVAL 3 DAY)
Note: no function used on the created column in where clause. This query should be able to use index.
Using MySQL
ID Date
001 2010-08-01
002 2010-08-15
003 2010-08-22
...
....
Query
select ID, Date from table where date < curdate + 7;
The above query is not working, it showing error.
How to get date upto nextweek date, I don't want to mentioned the date, it should calculate the systemdate + 7 days.
For Example
Today is 2010-06-30,
So it should take a value upto 2010-07-06
How to make a query for this condition....?
Using the DATE_ADD() function:
... WHERE date < DATE_ADD(CURDATE(), INTERVAL 7 DAY);
using an operator:
.... WHERE date < CURDATE() + INTERVAL 7 DAY
reference on date_add
I'm assuming that by curdate, you mean the function and not a column name. If it's a column name, change accordingly (although I wouldn't name a column after an existing mySQL function.)
Please try this
select ID, Date from table where Date < DATE_ADD(CURDATE(), INTERVAL 7 DAY)