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.
Related
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 have a dateAdded field that is a DATETIME type. I'm trying the query
SELECT * FROM table WHERE dateAdded > now() - interval 30 minute
It returns 0 rows even though there are a lot of rows created within 30 minutes. When I switch the greater than sign to a less than sign every row in the table is returned. I am using MySQL 5.6.28 and an InnoDB table. What reason could cause this not to work? I am doing a similar query on other tables with a DATETIME type and it works fine.
There seems to be a difference between the timezone used on the column (i.e. application inserting dateAdded data) and the timezone used on the client side issuing the query.
SELECT NOW() - INTERVAL 30 MINUTE proved that difference.
first of all, I know that my question is very similar to that one:
MySQL select rows from exactly 7 days ago
the difference is that my dates are stored in the database as a timestamp.
I know that I can use FROM_UNIXTIME to get the date from the timestamp, the thing is, in another answer I read that was very resource consuming (because the timestamp field has to be converted to date in all the records before comparing).
DATE(from_unixtime(timestamp)) = CURRENT_DATE()
Is there any optimized way to do this?
Turn it around: calculate the unix timestamp of the target date first and use that.
WHERE timestamp = UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)
MySQL should calculate that value once and use it all the time (needs testing though). If it doesn't, use a variable or code.
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").
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