I need to do some clean-up in a database and change some urls from http://www.domain1.com to http://www.domain2.com, so I am doing a query like this:
UPDATE mytable set url = REPLACE(url, '"http://www.domain1.com"', '"http://www.domain2.com"')
the double comma is needed as this strings are part of a JSON string
however, the problem is that in the DB the old url is for some reason escaped so I have something like this
http:\/\/www.domain1.com
if I run a query like these they don't match anything
select * from mytable where url like '%http://www.domain1.com%'
select * from mytable where url like '%http:\/\/www.domain1.com%'
and therefore the replace query doesn't work either, so what's the correct way to escape these strings?
You might try:
where url like '%http:\\/\\/www.domain1.com%'
To be honest, I don't understand how or why forward slashes would be escaped. Perhaps you are better off just using a wildcard:
where url like '%http:_/_/www.domain1.com%'
Related
I know very little about sql queries but I need a query to replace '\\' with '\/'
I have a Wordpress database where there are characters in a long text string that look like this in phpMyAdmin Browse.
'\\uploads\\photos\\'
It needs to be '\/uploads/photos\/'. I want a query to find '\\uploads\\photos\\' so I can make sure the query is working.
I also want another query to permanently replace '\\uploads\\photos\\' with '\/uploads\/photos\/'.
The below query that finds photos returns results, but that's as far as I got.
SELECT *
FROM `mytable`
WHERE `mycolumn` LIKE '%photos%'
Thank you forpas for the excellent solution!
For future readers:
When I migrated my wordpress database from local online, all in one migration plugin missed the paths in wonderplugin gallery that I am using. Thus my requirement for the query.
This double backslashed \ text was in a long string and I was concerned that there were more double backslashes that could get changed. So adding on to the solution provided by forpas, I more accuratly targeted the path text. mytable was actually named wp_nonxenon_wonderplugin_gridgallery, and mycolumn was named data. This is the resultant query that will save me much work in the future.
UPDATE wp_nonxenon_wonderplugin_gridgallery
SET data = REPLACE(data, 'wp-content\\uploads\\photos\\', 'wp-content\/uploads\/photos\/')
WHERE data LIKE '%photos%';
You must escape each backslash with a double backslash:
SELECT REPLACE(mycolumn, '\\\\', '\\/')
FROM mytable
WHERE mycolumn LIKE '%photos%';
Or you can update the table:
UPDATE mytable
SET mycolumn = REPLACE(mycolumn, '\\\\', '\\/')
WHERE mycolumn LIKE '%photos%';
and the column will contain the values as you want them.
See the demo.
So I have to find the urls which contains the word 'foo' for example . I want to do this with a regex like SELECT * FROM table WHERE url = regex. The url is https//... OR http://.... and I need to find the word foo for example in the url. What I actually want is a regex which will check if foo is in the hostname or in the path. Thanks for help!
EDIT :
I want to find a faster alternative with regex because LIKE '%foo%' is very slow on my table.
Use LIKE with the % wildcard operator.
SELECT * FROM table WHERE url LIKE '%foo%'
I've got an issue with a find/replace failing in phpmyadmin. I'm thinking this must have to do with the backslashes escaping the slashes in the URL in the database and I'm not correctly accounting for or escaping the backslashes in my query.
The data is stored like this in the wpiv_postmeta table in the meta_value column, with the slashes in the URL escaped:
(more data) [{"ddl-repeat-id":"4","logo":"https:\/\/example.com\/uploads\/2016\/01\/mypng.png"} (more data)
I need to remove the s in the URL. I'm using
UPDATE wpiv_postmeta SET meta_value = replace(meta_value, 'https:\/\/example.com', 'http:\/\/example.com');
but I get no matched rows when I simulate it in phpMyAdmin.
And I don't want to run this because it's too greedy:
UPDATE wpiv_postmeta SET meta_value = replace(meta_value, 'https:', 'http:');
How do I escape or unescape the slashes in my UPDATE query?
You have to double-escape, since the SQL parser removes one level of escapes while processing the query string:
SELECT ... WHERE foo LIKE '%\a%' // find any 'a' in the db
SELECT ... WHERE foo LIKE '%\\a%' // find any '\a' in the db
So you'd need
... REPLACE(meta_evalue, 'http:\\/\\/', 'http://') ...
I am having the following problem:
I have a table T which has a column Name with names. The names have the following structure:
A\\B\C
You can create on yourself like this:
create table T ( Name varchar(10));
insert into T values ('A\\\\B\\C');
select * from T;
Now if I do this:
select Name from T where Name = 'A\\B\C';
That doesn't work, I need to escape the \ (backslash):
select Name from T where Name = 'A\\\\B\\C';
Fine.
But how do I do this automatically to a string Name?
Something like the following won't do it:
select replace('A\\B\C', '\\', '\\\\');
I get: A\\\BC
Any suggestions?
Many thanks in advance.
You have to use "verbatim string".After using that string your Replace function will
look like this
Replace(#"\", #"\\")
I hope it will help for you.
The literal A\\B\C must be coded as A\\\\A\\C, and the parameters of replace() need escaping too:
select 'A\\\\B\\C', replace('A\\\\B\\C', '\\', '\\\\');
output (see this running on SQLFiddle):
A\\B\C A\\\\B\\C
So there is little point in using replace. These two statements are equivalent:
select Name from T where Name = replace('A\\\\B\\C', '\\', '\\\\');
select Name from T where Name = 'A\\\\B\\C';
Usage of regular expression will solve your problem.
This below query will solve the given example.
1) S\\D\B
select * from T where Name REGEXP '[A-Z]\\\\\\\\[A-Z]\\\\[A-Z]$';
if incase the given example might have more then one char
2) D\\B\ACCC
select * from T where Name REGEXP '[A-Z]{1,5}\\\\\\\\[A-Z]{1,5}\\\\[A-Z]{1,5}$';
note: i have used 5 as the max occurrence of char considering the field size is 10 as its mentioned in the create table query.
We can still generalize it.If this still has not met your expectation feel free to ask for my help.
You're confusing what's IN the database with how you represent that data in SQL statements. When a string in the database contains a special character like \, you have to type \\ to represent that character, because \ is a special character in SQL syntax. You have to do this in INSERT statements, but you also have to do it in the parameters to the REPLACE function. There are never actually any double slashes in the data, they're just part of the UI.
Why do you think you need to double the slashes in the SQL expression? If you're typing queries, you should just double the slashes in your command line. If you're generating the query in a programming language, the best solution is to use prepared statements; the API will take care of proper encoding (prepared statements usually use a binary interface, which deals with the raw data). If, for some reason, you need to perform queries by constructing strings, the language should hopefully provide a function to escape the string. For instance, in PHP you would use mysqli_real_escape_string.
But you can't do it by SQL itself -- if you try to feed the non-escaped string to SQL, data is lost and it can't reconstruct it.
You could use LIKE:
SELECT NAME FROM T WHERE NAME LIKE '%\\\\%';
Not exactly sure by what you mean but, this should work.
select replace('A\\B\C', '\', '\\');
It's basically going to replace \ whereever encountered with \\ :)
Is this what you wanted?
I have a mysql field that has text such as "http://www.example.com/hello.php" in the "url" column.
I need some sort of mysql query that will allow me to find urls that have two or more slashes. Of course using '%//%' will return all of my urls due to the http:// aspect of the URLs.
So how can I get mysql to only look at urls from the 7th character?
using underscore _ wildcard character to represent the first 7 characters (http://) followed by the pattern you are searching for e.g:
select * from urls where url like '_______%//%'; -- double //
or
select * from urls where url like '_______%/%'; -- single /
or
select * from urls where url like '_______%google%';
Regular expressions ? http://dev.mysql.com/doc/refman/5.1/en/regexp.html - you can do more with this
This should work:
SELECT * FROM table WHERE url LIKE 'http://%//%'
Using regular expressions would be a good way to go.
select * from urls where url regexp '[/].+[/].+[/]';