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").
Related
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'
I have a column in mysql table PACKAGES as SERVICE_EXPIRY_DATE which holds a date time value.
Now I want to match only those records whose SERVICE_EXPIRY_DATE is coming within the next 15 days from the moment of querying.
How can I match those columns.
You could do it by using intervals
WHERE `SERVICE_EXPIRY_DATE` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 15 DAY)
Look at MySql documentation for more reference https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html
I am trying to write an SQL query to return how many links were submitted to my website over the last 7 day period. So far I have this:
SELECT COUNT(`id`) AS `count`
FROM `links`
WHERE `created` > NOW() - 86400
AND `created` < NOW()
this works for one day, it returns one row called count with the number of links submitted in the last 24 hours. I need to change it to return 2 columns called date and count, with 7 rows (one for each day).
The tricky part that I can't get my head around is that created is a timestamp column, and I don't have access to change it so I have to work with it.
Edit: work in progress for the query:
SELECT DAY(FROM_UNIXTIME(created)) AS day, COUNT(id) count
FROM links
GROUP BY DAY(FROM_UNIXTIME(created))
LIMIT 7
NOW() actually shouldn't be working as it returns a datetime. Also, if you want to fetch 7 days worth of data, you want to subtract 604800 from UNIX_TIMESTAMP(). You can use then date and time functions with FROM_UNIXTIME. This will make grouping easier. Optimally, your column should be of datetime type.
It would go something like:
SELECT DAY(FROM_UNIXTIME(created)) day, COUNT(id) count
FROM links
WHERE created > UNIX_TIMESTAMP() - 604800 AND created < UNIX_TIMESTAMP()
GROUP BY DAY(FROM_UNIXTIME(created))
You can alternatively use the BETWEEN operator:
WHERE created BETWEEN UNIX_TIMESTAMP() - 604800 AND UNIX_TIMESTAMP()
See the demo
I have expiry date I want to check the Expiry date is less than current or not. and if the Expiry date is ex:27-03-2011. I want to do some operation if the expiry is ex:27-04-2011 no need to do anything.
I want to check and report the details for only one month,
after one month of the expiry date I don't want to check
how to do in query?
table1
field type
expdate date
Ex:2012-01-20
Please use MySQL date and time formats, they have the form YYYY-MM-DD (ISO date format). All MySQL Date and Time functions http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html work using this format.
MySQL also has date and time arithmetic. You can write expressions such as "NOW() - INTERVAL 5 day", which is also explained here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
root#localhost [(none)]> select now(), now() - interval 5 day\G
*************************** 1. row ***************************
now(): 2011-03-28 13:49:24
now() - interval 5 day: 2011-03-23 13:49:24
1 row in set (0.00 sec)
To list all items older than 5 days, use
SELECT id FROM sometable WHERE created > NOW() - INTERVAL 5 DAY
or a similar query. There are a few things to take note of:
an index on created should exist to make this fast
the column name created is not part of an expression in order to enable it to be used with an index. We are specifically writing 'created > NOW() - INTERVAL 5 DAY' and not 'created + INTERVAL 5 DAY > NOW()' - this expression uses the column name created in an expression, so no index usage possible.
if you are using this with a delete query, you are basically deleting old data from the left hand side of the time arrow, inserting new data at the right hand side of the time arrow. Maybe you want to use MySQL PARTITIONS and then PARTITION BY RANGE your table. That would allow you to delete old data by using ALTER TABLE DROP PARTITION throwing away the partition for say 5 days ago, creating a new one for tomorrow. This is much faster than DELETE in many cases.
I inserted a number of rows 3 hours ago and I don't want these rows to change. How can I write a sql statement that will compare current time with the timestamp in the row and restrict users from changing it if above criteria is met.
Thanks
If you want to do it by using mysql, you will have to use the INTERVAL statement which will allow you to "add" time to date functions... for instance:
UPDATE table
SET data = 'whatever'
WHERE NOW() - INTERVAL 3 HOUR < last_change
You can find more info and examples here: Date and Time Functions
You can use a WHERE clause in all updates:
UPDATE yourtable
SET foo = bar
WHERE inserttime > NOW() - interval 3 hour