MySQL database has wp_post
table in which the post_content
column is located.
I need to build a SQL request so that in all posts of the post_content
links of the type:
http://example.com/component/tags/tag/15-text.html
Replace with links of the type:
http://example.com/tag/text.html
That is, I need to replace all the characters that are between ‘example.com/
‘ and ‘text.html
‘, with ‘tag/
‘
taking into account the fact that the number in the link can be any one-, two- or three-digit (from 1 to 999 ).
If the wildcard ‘%
‘ could be used in the REPLACE
function, then the request could look something like this:
UPDATE wp_posts SET post_content = REPLACE(post_content, 'component/tags/tag/%-', 'tag/');
Unfortunately, this does not work. I have no experience with databases,
but it seems to me that the desired request can be created using SUBSTRING_INDEX
,
but I didn’t succeed in doing it myself. I would be grateful for any help.
More examples:
There are -> should be
http://example.com/component/tags/tag/15-text.html -> http://example.com/tag/text.html
http://example.com/component/tags/tag/1-sometext.html -> http://example.com/tag/sometext.html
http://example.com/component/tags/tag/2-anothertext.html -> http://example.com/tag/anothertext.html
and so on
http://example.com/component/tags/tag/999-moreanothertext.html -> http://example.com/tag/moreanothertext.html
What exactly needs to be done: to remove from these links part Component/Tags/Tag/Somenumber-
,
Where is Somenumber
is only Placeholder (Variable),
but everything else does not need to be changed
3
Answers
You need not just a replace, but a replace with regexp. Look at the request below.
This can be done via straightforward string replacement methods –
CONCAT
,SUBSTRING
,LOCATE
,REPLACE
. The first 3 queries show the step-by-step logic, while query #4 puts it all together.These queries assume
http://example.com/component/tags/tag/
is the same for all URLs. If that’s not the case, you could use similar logic to detect the position of/tag/
and useSUBSTRING
from there.The final query to actually update the table (query #6; note that duplicate URLs might exist after this and can be detected ahead of time with query #5 below):
Schema (MySQL v5.7)
Query #1 – remove the known/standard text before the filename (e.g. 15-thing.html)
Query #2 – find the index of – in the remaining string (e.g. 3)
Query #3 – remove everything after that index (e.g. thing.html)
Query #4 – construct the full URL (e.g. http://example.com/tag/thing.html)
Query #5 – detect duplicates ahead of actually updating the table
Query #6 – actually update the table
There are no results to be displayed.
Query #7 – view the new URLs
View on DB Fiddle
With the examples given: