0

We are using MySQL, and the table have a column named 'createdat` which means the time when the record are inserted.

Now we want to get data within 10 days from its created time.

Something like this:

select xx from table where time_of_now - createdat<= 10

I wonder if MySQL support this kind of query?

2 Answers 2

1

You can use DATEDIFF to get the difference between 2 dates in days, like so:

select xx 
from table 
where datediff(now(),createdat) <= 10

Documentation

Sign up to request clarification or add additional context in comments.

1 Comment

Using dateDiff is not such a good idea here because it will make the index on createdat unusable. The diff for all dates will be calculated first and then that will be compared with 10 (a linear scan)
0
select xx from table where createdat >= CURDATE() - INTERVAL 10 day

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.