mySQL update timestamp from 0 - mysql

I have this mySQL syntax which updates the table users and column skills_mod_time every time the page is reloaded. $sql = "UPDATE users SET skills_mod_time = CURRENT_TIMESTAMP";
How can i change from instead of using the CURRENT_TIMESTAMP to using a counting timestamp.
For example the column skills_mod_time is defaulted as 0. If the page was reloaded 10 sec later, i want the seconds part to be 10. Instead of the current way where it changes 0 to this present time.

It sounds like you want to log "seconds since login", or something similar, rather than "last activity." To do this, you should create a column called login_time, or similar, and only update it when a user logs in. You probably still want to track the last-activity time, as you do now, for the purpose of session expiry.
Then if you have these two columns, login_time, and last_activity, you can get the seconds between login_time and last_activity by subtracting the one from the other. Or you can get the time since login by subtracting login_time from NOW().
I hope this answers your question--if not, please clarify your question.

you can use timestampdiff function to take the difference of two timestamp values
something like
UPDATE users SET skills_mod_time = TIMESTAMPDIFF(MINUTE,skills_mod_time,CURRENT_TIMESTAMP);

Related

Delete Row at Set Time

I'm creating a database table where rows need to be removed after a set time. That time is defined in minutes by the valid_time cell in that row. I found this answer though I am not sure how I can implement what I need to into it.
Is someone able to tell me how I can implement this time (as minutes) into the event in the previous answer or if it's not possible, another way to do so. Thanks.
Clarification, I have two columns in the table. One is created which is a TIMESTAMP of when the row is created, and the second is valid_time, an integer in minutes of how long the row is valid for.
DELETE FROM table WHERE created < DATE_SUB(NOW(), INTERVAL `valid_time` MINUTE)
You can try to use the MySQL event scheduler and attach a DELETE query to it. That DELETE will be a simple query that will delete all records where current_time is greater that the valid_time/valid_until fields.
You can configure the scheduler to run in a minute/hourly/daily/... basis as you wish to erase the registers.
Check here and here for more information. M0rtiis offered the query example.

Sorting by time not working truly in MySql

I think title is clear what is my problem. I am working with MySql I just wanted to know what was the first registration on my website and when I sorted the table according to timestamp, the id of people who registered on the website were not true. The Image bellow will show what do I mean:
and this is the code what phpMyAdmin created:
SELECT *
FROM `table`
ORDER BY `table`.`timestamp` ASC
LIMIT 0 , 30
whats wrong with my table time? and why id is not ordered same as timestamps
If we assume that id is an auto-increment field, then this is almost certainly due to the fact that timestamp column is not assigned at the same physical time the id column is assigned. For example, if you have a multi-page registration from a website application, the initial object may be inserted into the database to contain the initial data fields gathered on the first page. But perhaps the timestamp is only set after the final page of registration is complete. So depending on the time the user takes to complete registration, the timestamp can be out-of-sync with the id order.
There are multiple ways to work around this. One way is to assign timestamp when the record is first inserted into the database, or you could use a DB insert trigger to populate the timestamp field instead.
There is also the possibility that timestamp represents the date and time the user last modified their profile (or otherwise caused modification of the database record). In this case, the timestamp could be changing to a "future" value while the id stays constant.

SQL - insert real time in a database table

I have a database and i need to create a table and insert in a column or columns the real time.
i will explain what exactly i want so maybe you can help.
I am making a game so i need with the pass of time a user to gain coins.
For example if the real time is 12:00 am i want in 12:15 the users to gain 50 coins (so a variable inside the database will change automatically after the pass of time), then in 12:30 they gain 50 coins etc.
and all that will happen even if they are online or offline. (i want database to work all time!)
In MySQL, the current time can always be retrieved by using the expression NOW().
Now suppose you want each player to get coins at a rate of 200 per hour. This happens to be one coin every 18 seconds; that's important because we will use integer arithmetic.
Make yourself a table user with these columns in it. I suppose you can also put other columns it it too.
user_id INT
current_coin_balance INT
last_autopay DATETIME
Then, every so often, run the following query:
UPDATE user
SET current_coin_balance = current_coin_balance +
ROUND(TIMESTAMPDIFF(SECOND,last_autopay, NOW()) / 18),
last_autopay = NOW()
If you wish you can add a WHERE clause, to only update some user or users in any given run of the query. Without the WHERE clause the query will handle all the users.
This will work out the number of seconds since the most recent payment and divide by eighteen to get the number of coins. It will then credit the user's balance and record the time when the payment was made.
It doesn't matter how often you run this query for any particular user. You can run it every time a user's record is touched, or every 15 minutes, or whatever you need.
You can run it as a so-called MySQL event (batch job) if you like.

Extract results updated in last 2 hours

I am trying to write a query to extract records updated in the last 2 hours in SQL Server 2008.
Could anyone help me write this?
select * from table where table.date1>=dateadd(hh,-2,getdate())
dateadd() function lets you subtract hours from getdate() letting you choose records updated past 2 hours
First, you have to design the table so you have a field where the time of the last change will be stored
Then, whenever you update a row, update the value in the 'last update' field. After that, you can use a script such as suggested by Vijaykumar
The downside of this method is that when a single record was changed more than once in the specified time period, you will be notified only about the time of the last update.
Another solution for tracking the updates is to read the database online transaction log file, but you'll need a third party tool for that

Increment a value for entire database

So we have a database. Every so often we'll add a new data set and the oldest dataset should be removed.
Say the DB is Early June: 18 records, Late June: 15 records, Aug: 23 records. When we add the next records we want to remove the Early June records. However adding new data isn't totally regular.
My second thought is to have an "oldness" field for each record and before adding a new data set, increment all of the "oldness" fields by 1, then removing all the data that has "oldness" of 3.
I'm worried that this is not the optimal way to do this, but I'm not sure it matters since it's a fairly small database that is updated infrequently. That said, I'm certainly open to suggestions for a better algorithm to accomplish this.
I'm assuming your data is stored such that it has a timestamp (date) column for each report, and that you always want to remove data that is x (in this case, 3) months old. If so, you might think about using mysql's DATEDIFF function.
For example, it might look something like this:
DELETE from table1 WHERE datediff(CURRENT_DATE(), datecol) > 89
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
Is it always by month?
UPDATE table SET status = "oldness" WHERE date_inserted <= NOW() - INTERVAL 3 MONTH
With a little help from user937146 I think I've got the solution. Basically
$query = "SELECT MIN(timestamp) FROM new_items WHERE type = 'Book' ";
Then it's just a matter of taking that timestamp, adding an hour (since the data can take more than a second to upload, probably one minute would be sufficient) and then deleting everything from the database older than that timestamp.