BrainPill Posted February 20, 2019 Share Posted February 20, 2019 I want to have a mysql command that deletes a row based on the present time value. For example the column is like: first_time = 1550566437 (the time in the table.column is based on time() in php.) I would like to delete the rows that are older than one hour from the present time. I want to use mysql to delete it. My idea is that you can use now() from mysql and determine the difference in seconds (3600) Is that possible and how is it done? Link to comment Share on other sites More sharing options...
BrainPill Posted February 20, 2019 Author Share Posted February 20, 2019 I also have a changed_on column with a timestamp value, is that also possible to use for the problem above? Link to comment Share on other sites More sharing options...
Funce Posted February 20, 2019 Share Posted February 20, 2019 An SQL command given that you already have the unix time stored in another variable would be quite simple. DELETE FROM table WHERE time < ? As long as you've inserted these records with their related timestamp, this should remove everything that has a timestamp less than the value you set. The value can be calculated as <?php $criteria = time() - 3600; //3600 seconds is 1 hour ?> Link to comment Share on other sites More sharing options...
BrainPill Posted February 21, 2019 Author Share Posted February 21, 2019 (edited) I solved this meanwhile. I used the timestamp column to make the query . I used this query : DELETE FROM `tab` WHERE `timestamp_col` < (NOW()– INTERVAL 10 MINUTE); I works only in the mysql command line interpreter. But I cant get it working in a batch script. Also in phpmyadmin it does not work. The error is in the minus sign. when running the batch script it shows a u with a caret like this û , and phpmyadmin gives error messages. Anyone any idea how to solve this? Edited February 21, 2019 by BrainPill Link to comment Share on other sites More sharing options...
BrainPill Posted February 21, 2019 Author Share Posted February 21, 2019 I made a type mismatch in PHPmyadmin. The ; should not be included inside the double quotes. But still I can't understand the change of : - into a : û in the batch script. What causes this error? Link to comment Share on other sites More sharing options...
BrainPill Posted February 21, 2019 Author Share Posted February 21, 2019 I also solved it for batch files, by removing the parentheses Link to comment Share on other sites More sharing options...
justsomeguy Posted February 21, 2019 Share Posted February 21, 2019 The now function returns a date, if that is an integer column then you should use the unix_timestamp function and just subtract the number of seconds. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now