Wondering if there is a way to do this without using two hits to the sql database.
If a person views content the timestamp is recorded.
If the person views the same content again 2 hours later the timestamp is not updated.
If the person views the same content 10 hours after first viewing the content, update the timestamp db table field.
Any method of doing this via SQL and not doing a "select" than php comparison than an "update" ??
update mytable
set lastvisited=now()
where person='john' and lastvisited<(now()-interval 10 hour);
Try
UPDATE tabel_name SET column_name=NOW() WHERE TIMESTAMPDIFF(HOUR, NOW(), column_name) >= 10
Related
I have in my DB (db_test) a table (tb_test) with 3 columns (id, test_field, timestamp_ins, timestamp_mod).
id is the 'primary key' with auto-increment attribute;
test_field is a char(1) that can contains only 2 values ('N' or 'S');
timestamp_ins is a datetime with current timestamp (not updating);
timestamp_mod is a datetime with current timestamp set to ON UPDATE CURRENT_TIMESTAMP().
I would like test_field automatically returns to default value ('N') 30 minutes after last modify of record indicated in timestamp_mod value.
I'm not expert in mysql, so I need help about this.
Is it possible using phpMyAdmin on a XAMPP virtual server?
--UPDATE--
Solved with this syntax:
CREATE EVENT IF NOT EXISTS test_event
ON SCHEDULE EVERY 1 MINUTE
DO
UPDATE ni0y2__test
SET test_field = DEFAULT
WHERE test_field < NOW() - INTERVAL 30 MINUTE
Only one doubt:
can this event make my DB performances worse?
I prefer putting the onus on the reader, not the table:
SELECT IF (timestamp_mod < NOW() - INTERVAL 30 MINUTE,
'N',
test_field) AS funky_field
FROM tb_test;
EVENTs have some overhead. Also, the above method will change at precisely 30 minutes; any attempt at using EVENT will be only approximately 30.
You could "hide" the IF(...) from the users by using a VIEW or Stored Function or a GENERATED column.
Accidentally, my host had set the time for 2012 and in my database, there are more than 4000 records updated timestamp column as 2012-11-21 11:24:40.
I cant change time on all columns while updating, I need to keep the same time as per the table column, I just need to change only year 2012 to 2016 in that table.
I listed all columns using this query,
SELECT * FROM `table` WHERE `del_date` LIKE '%2012-11-21%';
but do not know to change the year only in that column.
Please help how can I change the same.
You can use ADDDATE(), like
UPDATE yourTableName SET del_date = ADDDATE(del_date, INTERVAL 4 YEAR);
I am writing a MySQL Database and I wish to do the title within the MySQL database itself via Events if possible. I am updating the database via a Windows Service written in VB.net and have no desire to do the below via a separate 'server control' type program.
Basically, I have written the below in to the Database, but it doesn't quite do what I want:
Create Event MachineOffline
On
SCHEDULE Every 2 Minute
Do
UPDATE maindb.monitortable SET Online='1';
I would like it to say something like the below:
Create Event MachineOffline
On
SCHEDULE Every 2 Minute
Do
IF **RowX updated < 2 minutes ago** then
UPDATE Database.Table SET Online='0' where RowX(ID);
I have no idea how to achieve this hence my question to everyone. I have no desire to create
If you give your Database.Table an extra column, lets say, last_updated timestamp, you can then update it like so:
update Database.Table
set online=0, last_updated = now()
where last_updated <= now() - interval 2 minute
and online=1
I have Rails application which using MYSQL as database. For some condition, I have to delete all the records from table which was stored exactly 2 hours before the current time.
My query is :
DELETE FROM TABLE_NAME WHERE (NOW() - created_at) > 7200;
Here create_at is datetime column type. Storing the value in the format "2012-12-04 06:39:44"
My problem is, the above query fetch the records even though the record created time is just 40 to 50 minutes and got deleted. The only problem is the record got delete after it reach 40 to 50 minx from it create time.
Can any one please correct my query. I want the MySQL solution. Please help me
You probably need this if you want to delete records created exactly 2 hours ago:
DELETE FROM TABLE_NAME WHERE created_at = NOW() - INTERVAL 2 HOUR
or this, that will delete all records created more than 2 hours ago:
DELETE FROM TABLE_NAME WHERE created_at < NOW() - INTERVAL 2 HOUR
Try this ::
DELETE FROM TABLE_NAME WHERE TIMEDIFF(NOW(),created_at) < '02:00:00';
Try:
DELETE FROM TABLE_NAME WHERE created_at<DATE_SUB(NOW(),INTERVAL 2 HOUR)
This query will delete everything created MORE THAN 2 hours ago. Putting an equal sign would mean EXACTLY 2 hours ago (in second). Of course you can format date to consider only minutes, but that would slow down the query.
If created_at is indexed (and I think it should be) don't perform any functions on it so it can use index to perform delete faster.
I understand you want to delete all records created within a time lapse. So, you shouldn't apply a "greater than" operator to the subtract operation. Instead you should try to specify an appropriated time frame.
You could also take a look to the timediff function http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Sorry I'm not able to post the right statement for you, since I don't have a mysql server at hand.
I have a PHP application that stores PHP datetime to my MySQL table. I did not noticed that the php server is using wrong timezone settings, so all the date that is stored in the table is incorrect. My timezone is GMT+8 but the PHP 'date.timezone' was GMT+0, so I need to add 8 hours to all existing date value in the table.
current data:
ID,session_start,session_close,total_sale
1,2012-01-01 01:35:33,2012-01-01 09:35:33,8211.9
2,2012-01-02 01:32:31,2012-01-02 09:46:54,4323.56
3,2012-01-03 01:21:38,2012-01-03 09:32:21,8732.33
desired data:
ID,session_start,session_close,total_sale
1,2012-01-01 09:35:33,2012-01-01 17:35:33,8211.9
2,2012-01-02 09:32:31,2012-01-02 17:46:54,4323.56
3,2012-01-03 09:21:38,2012-01-03 17:32:21,8732.33
I need to fix the datetime for all the data in my mysql table (add 8 hours more to the original datetime value, to be precise).
I know this can be done by writing a simple PHP script to loop over all rows in that table and modify the datetime value as needed, but it will be fun (and cool too!) if somebody can show me this can be done in MySQL.
UPDATE tablename SET
session_start=DATE_ADD(session_start INTERVAL 8 HOUR),
session_close=DATE_ADD(session_close INTERVAL 8 HOUR)
WHERE
whatever