mysql, need to update column with random dates - mysql

I have a 'timestamp' type column in my table called updated_date. When adding a column to the table, all rows got updated to the same updated_date. Not a disaster as we're still in testing, but it kind of broke the functionality of our site (which shows things in order of updated_date).
Is there a way I can change all the updated_date values in the column (but where id is lower than x) to some random date (or an incremental date)?
Thanks in advance!

This might solve your problem:
UPDATE updated_table SET timestamp = FROM_UNIXTIME(1e9 + id) WHERE id < x;
Basically it sets dates to Unix timestamps corresponding to 1 billion + id (1,000,000,000 unix timestamp is 2001-09-08 21:46:40). That way you get unique timestamps in order of id.

Well, you could do this
UPDATE table SET updated_time = NOW() WHERE id < x
Given id belongs to table
in case you want some random data from the past
UPDATE test2 SET update_time = NOW() - interval rand()*120 day - interval rand()*36000 second WHERE id < x
Tweak it to your needs

Timestamps are just the number of seconds since the epoch (1970-01-01 00:00:01). If you start with a base timestamp, you can just add a random number of seconds since that number and you have random dates.

Related

Replace values in MySQL colums from row x to row y

I'd need to replace post dates from wordpress post table.
There are >800.000 post entries with the same date because of a migration.
How can I replace the date by "from row x to row"?
For example:
row 1 - 10.000 should have date 2013-01-02 09:20:10
row 10.001 - 20.000 should have date 2013-02-05 12:30:21
and so on...
Or maybe replacing by post id?
I know there is a sql query to do this, but I can not remember which one and how to use it correctly.
try adding a LIMIT to the sql to update rows:
UPDATE {table}
SET {datefield} = "{desired date}"
WHERE {datefield} = "{bad date}"
LIMIT 10000;
this will update 10000 rows at a time with a new date as desired, however it's not particularly picky about which ones get updated in which order, generally it will be in the database's internal order which is (roughly) chronological.
is there any other part of the data you can use to determine which records should be updated with which date?
This is not what you asked for, but might be better. You can create distinct timestamps, as if a post has been created every X seconds:
update posts
set created = timestamp('2013-01-02 12:00:00') + interval id * 140 second
where 1=1
http://sqlfiddle.com/#!9/a6c7e0/2
You can even make them look random:
update posts
set created =
timestamp('2013-01-02 12:00:00')
+ interval id * 140 second
+ interval floor(rand()*140) second
where 1=1
http://sqlfiddle.com/#!9/b394c/1

select where time = x or later

I have a table with columns username and timestamp . When a username is entered the current timestamp is as well.
I would like to fetch the rows of all the new users from a certain time until now, but the start time varies. Lets make it a timestamp represented by x.
I would like to use SQL to select all rows in a table which have a time-stamp in the time range of timestamp x up until now().
select * from tablename where timestamp between cast(x as timestamp) and now();
Try the statement you had placed in the comments but without the quotes. SELECT username FROM accounts WHERE timestamp > x
And for X you would enter time in 24hr format ie. 15:26

Substract MYSQL timestamp fixed amount of hour where ID is something

I want to make a line that removes a specific timestamp for a specific ID, I use MariaDB and havn'ut figured out how to.
X = some hour maybe 2 hours
TIMESTAMP = name of the table
Y = user id
What I want is something like:
Remove X amount of hours from TIMESTAMP where the id is Y
my timestamp format is not date it's like = 1414254628
below is on same table.
timestamp = 1414254628
timestamp2 = 1413646379
hope to get help, thanks!
sorry about the code blocks but but I had error on posting this thread so I had to make them look like codes
If I understand correctly and you store epoch time in your timestamp column you can just subtract the required number of seconds from it
SELECT timestamp - 3600 * 2 newtimestamp -- subtract two hours
FROM users
WHERE user_id = 1
Here is SQLFiddle demo
...how do i actually update the value?
By using UPDATE
UPDATE users
SET timestamp = timestamp - 3600 * 2
WHERE user_id = 1;
Here is SQLFiddle

Mysql updates based on time?

Is there a way automatically update some tuples based on time.
I have a field that I would like to increment every week from the time stored for that particular row.
Say I have two tuples with date and count fields:
2000-01-02 10
2000-01-03 1
Is it possible to automatically increment the count field every week from the stored date?
So that the first row is incremented on 2000-01-09 and the second row is incremented on 2000-01-10 and this would be done weekly.
Or in general can I update something automatically based on some time gone by?
Thank you.
You could store an extra field: next_increment_date.
Then you update regularly (say, once per hour or day... or however often makes sense):
UPDATE my_table
SET next_increment_date = DATE_ADD( next_increment_date, INTERVAL 1 WEEK ),
count = count + 1
WHERE next_increment_date <= NOW();
I think you want the event scheduler:
http://dev.mysql.com/doc/refman/5.1/en/events.html

MySQL : selecting data based on a timestamp interval

My dataset is a table with 3 rows : ID of a hard-drive, percentage of empty space, timestamp. The table is appended with the new state of each HDD (1200 of them) every 20 minutes.
If I want to pick the last state of my HDD pool, I go for a MAX(timestamp), and a MIN(timestamp) if I want the oldest.
But say I have a given timestamp, how can I ask MySQL to retrieve data from more or less X seconds around this timestamp ?
WHERE yourTimeStamp
between TIMESTAMPADD(SECOND,-3,yourtimestamp)
and TIMESTAMPADD(SECOND, 3,yourtimestamp)
where -3 and + 3 was substituted for X
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampadd for more help.
Like this:
WHERE timestamp BETWEEN DATE_SUB('<given_timestamp>', INTERVAL 5 SECOND)
AND DATE_ADD('<given_timestamp>', INTERVAL 5 SECOND);
As mentioned in the other answer, your query is slow when selection is based on the timestamp field.
You can add an INDEX on that column to speed it up:
ALTER TABLE <table_name> ADD INDEX(`timestamp`)
Note that, depending on the size of your table, the first time you add an index it takes a while. Secondly, it slows down INSERT queries and adds to the size of your database. This is different for everybody so you just have to find out by testing.