Replace values in MySQL colums from row x to row y - mysql

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

Related

Returning rows where there is no reconnect timestamp

I'm new to SQL, so please forgive me for what seems like a basic question.
I have a database called 'timestamps' that is structured like this;
ID
Connected_Unixtime
Disconnected_Unixtime
3
1658260585
1658260645
1
1658260465
1658260525
2
1658260345
1658260405
1
1658260225
1658260285
I'm trying to write some SQL that returns rows of IDs that disconnected and have had more than one hour since the last connect. So if there's an ID that has a Disconnected_Unixtime but no Connected_Unixtime after one hour of disconnecting, return the most recent row.
Basically the results should look like this, with just IDs that didn't have a new connection after an hour:
ID
Connected_Unixtime
Disconnected_Unixtime
3
1658260585
1658260645
2
1658260345
1658260405
I tried to use the not exist operator since a new row is created every time an ID connects. This is what I came up with, but it's not correct.
select *
from timestamps
where not exists (select *
from timestamps
where (Disconnected_Unixtime) > now() - interval 1 hour
order by Disconnected_Unixtime desc
)
Thanks for the help.

How to create MySQL query to return records older than a predefined date in a separate table

I am in the process of creating a maintenance query for a MySQL table where I need to mark records as deletable (Update a column value from 0 to 1) based on a date field in the table.
Pretty simple but the problem I have is that the criteria to mark the record as deletable varies depending on the account ID (acc_id column).
For example, the table will have records from multiple accounts (acc_id field) and there is a separate accounts table that has a column (retention_age) that I need to use to determine whether a record can be marked as deletable.
Example:
SELECT *
FROM files
WHERE DATE(file_downloaded_date) < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
AND deleted = 0;
So for example, in the table with the records to update, there may be 100 records for acc_id = 1, 250 records for acc_id = 2 etc.... acc_id 1 is set to mark records as deletable after 14 days, acc_id 2 is set to mark as deletable after 30 days.
I'm trying to figure out if I can create an SQL query to do this all in one query or if I'll need to create a separate query for each account.
I'm thinking that if there would be a way to replace the '30' in INTERNAL 30 DAY, with the retention_age value for the acc_id of each record that would be the best way but I don't know how to do that or if that will even work.
Any suggestion would be greatly appreciated.
You should be able to achieve this using a LEFT JOIN to get the respective account and the use its retention_age as the INTERVAL:
DELETE files FROM files
LEFT JOIN accounts ON accounts.id = files.acc_id
WHERE
DATE(file_downloaded_date) < DATE_SUB(CURDATE(), INTERVAL accounts.retention_age DAY)
AND deleted = 0

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

Incrementally update date field column based on order of a second column

MSQL DB for WordPress site.
I would like to update a date column for each row in a table, setting the date/time to an incrementing value (e.g. increment value by 1 day). The order that I do the updates should be based on the textual sort of another column in the row. The actual date value used isn't relevant (it could start at 2001-01-01 00:00:00 and increment in seconds, minutes, hours or days).
Example of two columns in the table:
field_date , title_text
2014-08-20 09:00:27, AAA-Entry
2014-08-24 10:00:00, ZZZ-Entry
2014-08-27 10:15:00, MMM-Entry
So, the row with the first 'title_text' (alphabetical sorting) should get the newest date. The next row based on title_text should get a slightly older date and so on until the last row (based on title_text) has the oldest date. So the data shown above would end up looking something like:
field_date , title_text
2000-01-03 00:00:00, AAA-Entry
2000-01-01 00:00:00, ZZZ-Entry
2000-01-02 00:00:00, MMM-Entry
After the update then the command:
select * from tbl_name order by field_date
would show output that would also be in alphabetical order of the title_text field. This would be equivalent to running:
select * from tbl_name order by title_text
I'm looking at:
[Incrementing datetime field with an update statement for SQL2005 and trying to get this to work on mySQL as I think this would work (I just change the 'order by' statement to 'order by title_text'). But I'm having problems with converting this to mySQL.
I'd appreciate any suggestions on different approaches or with getting the above solution to work in mySQL.
Thanks
Ian
Note: The reason behind this is that Wordpress populates list boxes/search results, based on the posting date, but I want it to be ordered based on the title text. There may be a 'Wordpress' answer (or plugin) to do this (and if you know, please feel free to tell me ;-) ), but from a learning viewpoint, I'd also like to understand how this was possible using mySQL.
In MySQL, you can do updates with sorts. The easiest way is to define a variable first and then do the update:
set #rn = -1;
update t
set field_date = date('2000-01-01') + interval (#rn := #rn + 1) day
order by title_text desc;

mysql, need to update column with random dates

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.