How to query data for a certain data range in MySQL - mysql

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?

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

select xx from table where createdat >= CURDATE() - INTERVAL 10 day

Related

Get last 3 months record from paid salary table where month and year is stored in separate column

I need help with the MySQL query. I have a table like this in MySQL
What I want is to get the last 3 months' records from the table. But I don't understand how to make this MySQL query.
I have tried to contact both columns and tried to generate a MySQL date to compare but it returns me null.
I also tried this query and it works for current data. But i am not sure if it is the only way to do this or is there any better way.
What I want is to get the last 3 months' records from the table.
I think you want a where clause like this:
date(concat_ws('-', salary_year, salary_month, 1)) >= (curdate() - interval (1 - day(curdate()) day) - interval 2 month
The key idea here is to convert the string to a date so it is handled correctly.
If you have a fixed date, then you would represent that as:
date(concat_ws('-', salary_year, salary_month, 1)) >= '2021-04--01'

how to convert from one date format to another in mysql

I am using MySQL and I want to get all the dates in the previous week from a column, say xyz, which is of type date and has the format YYYY-MM-DD. However, when I use
select
*
from
tablename
where
xyz > date_sub(curdate(),INTERVAL 1 week);
I get dates which are not within the past 1 week. I get dates which are one month from now and some random dates.
You query is pulling all records in which xyz (your date column I assume) is newer than one week ago. This could in fact be future records. If you want to exclusively view things from the previous week, you need to use the between keyword.
select
*
from
tablename
where
xyz between date_sub(curdate(), INTERVAL 1 week) and curdate()

Delete an entry from a table once it's been there for more than x days

I have a system where you can get something for "free" but only once every 7 days, I'm currently having an issue in that once every 7 days part.
What I want to do is delete entries in a certain table once that one or more entry/entries went over 7 days. The concerned table has an ID, USERNAME and DATE column.
Any thoughts?
It should be as easy as:
delete from theTable where date < now() - interval 7 days;
Make sure to run it often enough so that you don't have to delete to many rows.
If you're in an environment without replication you can go ahead and add a limit
delete from theTable where date < now() - interval 7 days limit 1000;
And if this is a large table put an index on date (or where date is first) so it doesn't do a table scan.

Checking time-stamps if they are from today in MySQL

I have a database with several tables. Each of these tables has a column 'created' which contains time-stamps of when that particular row was created in the database.
Now, I want to create a MySQL script that checks once every week if there is data coming into these tables. So, there should be data coming everyday. How do I create a MySQL script that allows me to do this for all the tables in the database?
Note: Remember I want to do this for all the tables in the database with a single script. That's the main thing I want to know.
i use this approach for a table called call, with a column of timestamp type called systemdate:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW());
mysql DATE() statement gets the datepart of a datetime or timestamp field.
Sorry, just noticed that you want to check if atleast there is an entry for each of the days in the previous week.
you can use this query to check the prevous days individually:
yesterday:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW()) - 1;
before yesterday:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW()) - 2;
Or you can check the whole week at once:
SELECT * FROM `call` WHERE DATE(`systemdate`) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) GROUP BY DATE(`systemdate`);
This will return one result for each day, so if you have 7 results you'll know at least an entry was made on each day.
select * from table
where created between subdate(current_date, interval 7 day) and current_date;
Selecting datetimes up to current_date includes everything up to the start of "today" (ie "the previous midnight").

how to get minutes from two different date in mysql

I am having fields called 'timestamp','onlineuser' from table called 'User'. I need to select onlineuser who is having the timestamp which is less than 5 minutes between NOW(),timestamp.
For example, timestamp will be like 2011-06-20 16:02:22.
I tried with mysql query , but seems wrong. kindly help me
SELECT * FROM user WHERE timestamp >= NOW() - INTERVAL 5 MINUTE