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
Related
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.
I have table tbl_dtcount. In that table there is one column for date.
Now I need to reduce one day for each and every rows in that date field. The date is beginning from 2012-05-19 to 2012-07-03. What is the MySQL update statement to perform this?
How about this.
Update tbl_dtcount
set mydate = DATE_SUB(mydate, INTERVAL 1 DAY)
where <conditions>;
UPDATE table_name
SET date_column = DATE_SUB('1998-01-02', INTERVAL 1 DAY)
....
see detail MySQL DATE_SUB
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'm trying to offset a timezone error from PHP. All times recorded in table 'test' was ahead by two hours. What I want is to update each record by minusing two hours from the time that is already there.
I tried:
UPDATE test
SET LastModifiedDate = SUBTIME( LastModifiedDate, '02:00:00' )
But this just updates all fields with the same value.
Please assist
tthanks
update test set LastModifiedDate = LastModifiedDate - interval 2 hour;
Use the DATE_SUB() function:
UPDATE test SET LastModifiedDate = DATE_SUB(LastModifiedDate, INTERVAL 2 HOUR)
Test it first to be certain it's doing what you want:
SELECT LastModifiedDate, DATE_SUB(LastModifiedDate, INTERVAL 2 HOUR) FROM test;
update test set LastModifiedDate = adddate(LastModifiedDate, interval -2 hour);
this will modify all your dates to -2 hour. you can narrow down the result in "where" section of the query by targeting specific rows.
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.