4

I have a few thousand MySQL records that contain strings of text saved in a MySQL database that contain unique values after a URL variable called "additional_development" with data like 'Chestnut%20Estates' in each record:

<a class="button" href="http://www.websitedomain.com/search-results/?fullinfo=&Location=all&additional_development=Chestnut%20Estates&additional_postal_code=

They dynamic value is always in the &additional_development= variable. If there is no value here it simply looks like this additional_development=&additional_postal_code=

I need to remove the dynamic text from this location to the ?fullinfo= variable without knowing the contents (Chestnut Estates) to move, only the location of where this value resides in the URL.

Desired Effect:

<a class="button" href="http://www.websitedomain.com/?fullinfo=Chestnut%20Estates&Location=all&additional_development=&additional_postal_code=

Using this question, I found I could not do this with regex. So I've attempted different degrees of Update commands with still no effect.

Any suggestions on the best way to achieve the desired result in a MySQL query?

Table name is wp_posts and column name is post_content.

2
  • only the location of where this value resides in the URL --> Does this means that in all your URLs you ALWAYS have the same parameters in the same order? I see a way to do this with SUBSTR_INDEX but I need a confirmation first. Commented Jun 12, 2017 at 8:36
  • It's always in the same order but the values are different for different subdivisions. Commented Jun 12, 2017 at 11:56

3 Answers 3

3

UPDATE it in chunks by adding a WHERE id < whatever since it will be pretty slow.Use it as a SELECT before to make sure you get the desired results.

UPDATE t SET col = CONCAT(
    SUBSTRING_INDEX(col, '&Location', 1),
    SUBSTRING_INDEX(SUBSTRING_INDEX(col, '&additional_postal', 1), 'additional_development=', -1),
    REPLACE(
        SUBSTRING_INDEX(col, 'fullinfo=', -1),
        SUBSTRING_INDEX(SUBSTRING_INDEX(col, '&additional_postal', 1), 'additional_development=', -1),
        ''
    )
);

See it working

Sign up to request clarification or add additional context in comments.

2 Comments

SUBSTR_INDEX was what I wanted to do, then I see that you were there first :) This is the the right pure SQL solution for sure
@Mihai -This works great unless there are multiple, similar instances of the string in the same record. It removes everything except for the last instance? How do I overcome this? See example: rextester.com/WPFZ46684
2
+250

Summary

The SQL below uses MySQL's INSERT string function to replace part of a string. It could be done in one query rather than two if needed but the SQL would be less readable.

Demo

http://rextester.com/RTKA97873

SQL

UPDATE wp_posts
SET post_content = 
  INSERT(post_content, 
         /* Replace from character after ?fullinfo= */
         LOCATE('?fullinfo=', post_content) + 10,
         /* Replace up to the next & */
         LOCATE('&', post_content, LOCATE('?fullinfo=', post_content) + 10) 
         - LOCATE('?fullinfo=', post_content) - 10,
         /* Replace with value of &additional_development parameter
            (i.e. substring after the = and before the next ampersand) */
         SUBSTRING(
           post_content,
           LOCATE('&additional_development=', post_content) + 24,
           LOCATE('&', post_content, LOCATE('&additional_development=', post_content) + 24)
           - LOCATE('&additional_development=', post_content) - 24));

UPDATE wp_posts
SET post_content = 
  INSERT(post_content,
         /* Replace from character after &additional_development= */
         LOCATE('&additional_development=', post_content) + 24, 
         /* Replace up to the next & */
         LOCATE('&', post_content, LOCATE('&additional_development=', post_content) + 24)
         - LOCATE('&additional_development=', post_content) - 24,
         /* Replace with empty string */
         '');

4 Comments

I think this will work but I need to include/locate more of the string as it's making changes I don't want. I've updated my original question, can you please review and update your answer with what is needed?
Sure - have just updated my answer accordingly (and fixed the demo link).
Close but it's still missing this portion? How does the updated answer match these aspects: <a class="button" href="websitedomain.com/search-results/?fullinfo=
Sorry - not quite clear on what you're asking here. The only difference between the result in the demo and the result asked for in your question is the latter is missing /search-results. As your q didn't mention wanting this to be removed I'd assumed it was just a typo - did you actually want this to be removed?
0

Is additional_development the only dynamic part? If so this is a simple string replace:

UPDATE table_name SET column_name=replace( column_name, '?fullinfo=&Location=all&additional_development=', '?fullinfo=' )

Otherwise it looks like you will have to implement this in another way, either by implementing a regular expression replace yourself in MySQL (there are plenty of examples around), or doing it in your programming language of choice via select, regexp in language, update. It is slower to do it that way of course, but you don't have much choice (other than switching databases).

Sorry

2 Comments

Yes, but it's the neighborhood name value after &additional_development= and before the &Location= that is dynamic. I need to move that to after ?fullinfo= I don't see how your UPDATE actually moves the data found in those locations to the new location?
The query I have wipes out whatever is in the location= query parameter. From reading your question it wasn't clear to me that that part was supposed to move as well. I thought you were just trying to move additional_development to fullinfo, which my simple replace does do (by destroying the location part). sorry it doesn't help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.