This may come across as silly, but if the CMS ignores subsequent <!--more-->
tags, you shouldn't really care if it adds one after every </em>
closing tag. Sure it may make the posts slightly larger than they should be, but since they're just going to be ignored, it seems silly to spend two days trying to not do that when at the end of the day it doesn't really matter whether you do or not.
That said, I am not a MySQL guy, but looks like this is as decent an approach as any, from this StackOverflow answer:
UPDATE wp_posts
SET post_content = CONCAT(REPLACE(LEFT(post_content,
INSTR(post_content, '</em>')+4), '</em>', '</em><!--more-->'),
SUBSTRING(post_content, INSTR(post_content, '</em>') + 5))
WHERE INSTR(post_content, '</em>') > 0;
or the (slightly simpler):
UPDATE wp_posts
SET post_content = CONCAT(LEFT(post_content, INSTR(post_content, '</em>')-1),
'</em><!--more-->',
SUBSTRING(post_content, INSTR(post_content, '</em>')+ 5))
WHERE INSTR(post_content, '</em>') > 0;
Before you do that you may want to check to see how many rows this will affect:
SELECT COUNT(*)
FROM wp_posts
WHERE INSTR(post_content, '</em>') > 0;
You'll probably want to add a WHERE
clause to only identify those posts that actually contain an </em>
tag (and you may need to define the requirements for what to do in those cases).
You don't have to check what value you could use in "deleted" if you have a way of generating unique integers, and MySQL does... the UUID_SHORT()
function generates a unique and monotonic BIGINT UNSIGNED
value every time it's called.
The function is good for 16.7M new values per second, every second, and will never return a duplicate (or smaller than previous) value as long as the @@SERVER_ID
remains the same and you don't shut down the server, set the system clock backward in time, and restart the server... and even if you do, the odds of duplicate values are still very small, but no longer truly impossible.
You could also make "deleted" be a DATETIME(6)
or TIMESTAMP(6)
on MySQL 5.6, using '0000-00-00 00:00:00.000000' to mean "not deleted" and NOW()
to mean deleted.
Try as I might, I have not been able to get MySQL to produce a NOW()
collision down to the microsecond.
Best Answer
Test: