First off, please note that this is not answered in the question here: MySQL update table based on another tables value or indeed any other Stack question or answer that I could find!
I converted a forum on a website and now just need to repair the internal URL links on a few hundred of the posts.
Posts are found in the column post_content within the table xyz_posts
ID post_content
1467 This is great https://example.com/index.php?topic=1234 I really like it
1468 Hello world
1469 Take a look https://example.com/index.php?topic=5678.0
You can see the URLs are mostly buried in the post text. Note that the example above can sometimes be written with topic=1234.0 at the end, although it is actually stored in the database as a value of 1234. I don't want to rewrite the url and accidentally keep the .0
Here's an example of how I need that table to look:
ID post_content
1467 This is great https://example.com/finished-page/ I really like it
1468 Hello world
1469 Take a look https://example.com/another-page/
So, the table xyz_converter maps the old topic number to the new post ID like this:
meta_key meta_value value_id
_bbp_old_topic_id 1234 15675
_bbp_old_reply_id 1234 17439
Caveat here, the number 1234 also exists in this table for forum replies which we don't want, but the meta_key and value_id are different for those as shown. This SQL query works to get to the right one:
SELECT * FROM `xyz_converter` WHERE meta_key LIKE '_bbp_old_topic_id' AND `meta_value` LIKE '1234'
Also in table xyz_posts we map the value_id mentioned above to the post's URL suffix like this:
ID post_name
15675 finished-page
15676 another-page
How do I construct a SQL query that will detect the meta_value in one table and then replace it with the correct final URL as mentioned?
PROCESS SUMMARY
- Detect the URL in xyz_posts post_content
- Extract the topic number from the URL (eg 1234). If it's 1234.0 then take just 1234
- Convert it to a post number in found in xyz_converter value_id (eg 15675) ensuring it is found alongside _bbp_old_topic_id
- Take the suffix of the URL in xyz_posts post_name (eg 'finished-page')
- Rewrite the original URL to include the suffix (not forgetting the trailing slashes).
I'm using MySQL 5.7.29 and PHPMyAdmin. I'm fairly new to SQL queries and a Regex noob, but willing to learn more!