Replacing some URLs with another - mysql

I have a MySQL 5.7.29 database on which a website is built. I need to construct a query to find all table rows containing lines such as
https://example.com/index.php?topic=7989.0
or similar and replace them with
https://example.com/my-redirect-page/?7989.0
The wildcard here is the ?topic=7989.0 as this can be something like ?topic=1234 or even ?topic=3456.0#anchor
I can display the rows they appear in (in PHPMyAdmin) using this (thanks to 'sticky bit' below) :
SELECT * FROM `abc_posts` WHERE `post_content` LIKE '%example.com\index.php?topic%'
My problem is that I then need to change just that URL when there is also text content around it.
Thanks in advance.

The question mark doesn't need to be escaped.
But 'https://example.com/index.php?topic=7989.0' isn't LIKE '%example.com\?topic%' as the question mark doesn't immediately follow the host name.
Try:
...
post_content LIKE '%example.com/index.php?topic%'
...

You could do something like thin to find them
SELECT 'https://example.com/index.php?topic=7989.0'
WHERE 'https://example.com/index.php?topic=7989.0' REGEXP 'example.com/index.php?topic=';
Which would find all rows. But for replacing it, you must also tell which database version youh have mysql 5.x have not many regex fucntions mariadb and mysql 8 have much more

Related

Having trouble with MySQL wildcard characters

Im following the instructions on w3schools but something appears to not be working. I keep getting the result "empty set" even though it seems like multiple names should be returned since there is names that start with a,l and c.
BTW I am using MySQL via the terminal on a mac. Thank you.
here is a picture of the table I'm using
Here is my syntax -
mysql> select * from Student where Name like '[alc]%';
With your select you search for a patter that exactly matches '[alc]'
followed by anything denoted by %.
Try regexp like this.
select * from Student where Name regexp '^(a|l|c)';
This will give you names that start with the given letters, not case sensitiv

Using IN and LIKE in a search and replace query

I have some fields in a MYSQL database with the following content:
eq":"fieldname1+fieldname2+fieldname3+fieldname4/4
The numbers are always different, so it could also be something like:
eq":"fieldname11+fieldname22+fieldname8/10
I would like to run a query to achieve the following:
eq":"((fieldname1+fieldname2+fieldname3+fieldname4)/4, 2)
I currently have the following query
UPDATE wp_wrdydtdbww_cp_calculated_fields_form_settings
SET form_structure = REPLACE(form_structure, '???', '???')
WHERE id IN (1,2,3);
The problem is, that there are a lot of additional strings, containing 'fieldname' or '/' as well, so I need to replace the exact structure.
Can somebody help me to modify it?
I tried something with the LIKE pattern (%), but can't get it to work as I always replace other parts of strings as well.
Thanks!

MYSQL Find entries that contain more than 7 numbers

I need to find entries that contain more than 7 numbers in one of my mysql tables BUT the numbers are separated by letters or anything else.
What I have is this little piece of code I use to find entries like dsc123456789:
select * from crawl where title regexp '[0-9]{7}'
How can I find entries like dsc-123-456_78B9? I tried different things but without success so far.
Thanks
You can use the following solution:
SELECT *
FROM crawl
WHERE title REGEXP '(([^[:digit:]])?[[:digit:]]){8,}';
Why the original query of the answer doesn't work?
-- this query doesn't work!
SELECT *
FROM crawl
WHERE title REGEXP '\d([^\d]?\d){7,}'
MySQL can't use character groups like \d (digits). So the query fails every time. On PHP and other languages the regular expression would look like this:
\d([^\d]?\d){7,}
but on MySQL this isn't valid. So you have to use the character classes of MySQL to solve this:
(([^[:digit:]])?[[:digit:]]){8,}
Hint: Make sure you use {8} or {8,} instead of {7} since you want to find all entries with more than 7 numbers / digits.

Apostrophe LIKE search in MYSQL not working

In MYSQL database, the title row is this:
Interview: Gus O\'Connor win
I am trying to search
title LIKE '%O\'Connor%'
I tried everything and still came up empty, no results returned.
I have tried
'%O''Connor%'
and
\"%O'Connor%"\
and yes, i am protecting myself from sql injection with code above.
You may try like this:
title LIKE "%O\\'Connor%"
If your database entry actually contains that backslash, you'll need to use this:
title LIKE "%O\\'Connor%"
Not sure why you are setting up a SQL string like this given the new PDO frame work which does allow not this syntax but anyways, try
LIKE "%O\\'Connor%"

Replacing a formatted string in MySql

I'm trying to replace all instances of an old BB tag markup in a MySql database with a newer, slightly different one.
The old format is this...
[youtube:********]{Video ID}[/youtube:********]
Which I would like to replace with this...
[youtube:********]http://www.youtube.com/watch?v={Video ID}[/youtube:********]
Where the *'s are a random string of alpha-numeric characters. So simply REPLACE(feild, '[youtube:********]', '[youtube:********]http://www.youtube.com?watch?v= won't do unfortunately.
All the clumsy attempts I've made using REPLACE() and INSTR() have resulted in nasty things like [b]Bold Text[/b]http://www.youtube.com/watch?v=
Is there a way to do this kind of pattern replacement in MySql? Possibly with Regular Expressions?
Thank you.
Is this what you tried?
UPDATE table SET Field = REPLACE(Field,']{',']http://www.youtube.com/watch?v={')
This would depend if there isnt any other occurences of ']{'
EDIT: You may also want to try:
UPDATE table SET Field = LEFT(Field,#) + 'http://www.youtube.com/watch?v='+
RIGHT(Field,(Char_Length(Field)-#);
Just check the syntax with MYSQl docs. Char_LNEGTH() may need to be LENGTH() - im sure you get the idea