How to delete a specific string value from multiple tables on phpmyadmin? - mysql

I had a customer that installed a malware plugin which has already been cleaned.
The problem now it that i have this string - https://https;//main.travelfornamewalking.ga/stat.js?s=newrq - on the 2000 posts I have on the webiste.
How can I delete this specific string on each post bulk?
Keep in mind I don't want to delete the content. The problem is the malware added a script with that link on all the entries of the wp_posts table.... :(

You have to find out the table and the column that contains the problem link.
Once you have found the table and the column, check how many rows are affected. This will also give you an overview of what you could replace the problem link with.
First, the most important thing: Make a copy of the table! If you make a wrong update, you will need it.
// Maybe just look for a part of the problem link, it could be here in another form or with other parameters, good for checking.
SELECT ColumName FROM TableName WHERE ColumName like '%main.travelfornamewalking.ga%'
To replace the problem link, use REPLACE(), e.g. overwrite with an empty string.
https://www.w3resource.com/mysql/string-functions/mysql-replace-function.php
REPLACE(str, find_string, replace_with) -> full text of problemlink with all parameters.
UPDATE TableName SET ColumName = REPLACE(ColumName, 'https://https;//main.travelfornamewalking.ga/stat.js?s=newrq', '')
-> Repeat the select and update for all forms of this problem link that you are replacing.

Related

SELECT Showing me a list with the name of the column and not the data inside

I have quite a strange problem, I'm very new to SQL and I was doing a free course in SQL
I'm actually learning the "SELECT" command.
So, I create my database, create some table, put some data in it with the "INSERT INTO" command
And now I want to select some data in it, but I have a strange bug (not an error) when i do
SELECT * FROM aliment;
everything work like it's supposed to do, but when I do
SELECT 'nom','calories' FROM aliment;
Something strange happens.
Instead have a list of all the specific data i'm looking to get i just get a list with all the data replaced by the name of the columns.
Here 2 screen to show you what's happens:
[2
I know one it's from the terminal(and yes it's not mine but from a video) and mine is from the software, but it's still had to work no?
You have a typo in your SQL. Use backticks (on the same key as ~ on a US keyboard) around your column names, not '. Using a single quote (an apostrophe) makes it a literal value, not a column name.
SELECT `nom`,`calories`FROM aliment;

How to check if a db field contains a non breaking space?

I import data from an old DB and want to check in my new db if a field contains a non breaking space somewhere. All solutions I found so far replace it, but I just want to search via sql in my mariaDb if in any row this field does contain the non breaking space.
Searching with like obviously doesn't work e.g.
select * from my_table
where
my_field like "%CHAR(160)%"
All solutions I found want to replace like this REPLACE(The_txt, NCHAR(160), ' ') but I want to check if I have the problem at all before messing with the db.
As already answered by #Akina, thanks for your help, the below query helps to find records with non-breaking space -
SELECT * FROM <table_name> WHERE INSTR(<column_name>, CHAR(160));
Or
SELECT * FROM <table_name> WHERE LOCATE(CHAR(160), <column_name>);

SQL Replace some text of each string of a column

I have a database table that contains a column named: "shortLink".
This column contains a short link of an address of each row in the table.
I use the tinyurl.com services for the short links. The short link looks like this: https://tinyurl .com/randomletters.
Recently I figured out that I need to change the shortlinks to their preview short link version: https://preview.tinyurl .com/randomletters.
The only difference between the two link formats is that there is a prefix preview. between https:// and tinyurl.
Since I have hundreds of rows in the sql table I cannot fix this manually.
Is there any way to convert each shortlink (by adding the prefix preview. in the address) to its preview format with code in sql?
Thanks.
PS - Notice that there is a gap between tinyurl and .com in the link formats above. This gap is added intentionally because the forum would not let me publish the question otherwise.
-- this will update the field for you where it does not already have the preview. in it.
UPDATE YourTable
SET shortlinks= REPLACE( shortlinks, 'https://tinyurl .com', 'https://preview.tinyurl .com')
WHERE shortlinks NOT LIKE 'https://preview.tinyurl%'
You can just use an update:
update t
set shortlink = concat('http://preview.', substring(shortlink, 8))
where shorlink like 'http://tinyurl%';
Bro try this. first, remove all spaces in URL on anywhere then replace 'tinyurl' to
'preview'
UPDATE [Table_Name]
SET shortLink= REPLACE(REPLACE( shortLink, ' ', ''),'tinyurl', 'preview')

mysql to update a database using UPDATE SET and TRIM(LEADING wildcard prefix in record

In my database I have a table called 'content' and a field called 'link' and there are almost 300,000 records in that table.
In the field called 'link' there are a number of records that look like this :
http://www.example.com/blah/blah/123456789/url=http://www.destination.com
Unfortunately the prefix part of the records are individually unique where the numbered portion is constant changing from 90 to 150 alpha-numeric characters
I would like to remove the prefix up to and/or including the url=
So that the only thing left in the record is :
http://www.destination.com OR
I could even work with
url=http://www.destination.com
and simply do a replace command against the "url=" part as a second mysql command.
If there was a wildcard command, this job would be much easier and I would just wildcard everything showing up in the link record between :
http://www.example.com/blah/blah/ wildcard url=
But as everyone knows... there is no such wildcard available
So it had me looking at the UPDATE, SET and TRIM(LEADING commands
UPDATE content
SET link =
TRIM(LEADING 'url=' FROM link)
But this DID NOT generate the changes I wanted
And so I took the labor intensive method of downloading the database and using a Search and Replace program to make the changes to the 44 thousand records that contained these parameters.
But I would love to find a command that I could simply pass to the database to make this simpler in the future.
Any thoughts on how to accomplish this change in the future would be greatly appreciated.
Thanks in advance ;
You can use the SUBSTRING_INDEX function:
UPDATE content SET link=SUBSTRING_INDEX( `link` , 'url=', -1 )
I have not tested it, so I would recommend you check that substring_index returns the desired string first.
Assuming that the part you want to keep always begins with 'http://' you could get the desired result string with the help of the SUBSTRING_INDEX function:
SELECT CONCAT('http://', SUBSTRING_INDEX(link, 'http://', -1)) FROM content;
and fix your table with the simple statement
UPDATE
content
SET
link = CONCAT('http://', SUBSTRING_INDEX(link, 'http://', -1));
Explanation:
SUBSTRING_INDEX with third parameter negative returns the substring from the last occurence of the needle in the second parameter to the end. Because 'http://' isn't included in the return value, we add it again.
Remark:
If you've got https:// urls too, you should be able to adapt my solution.

using REPLACE in MySQL Query just isn't working

Quick background, I have a small database with a table named 'songs'. This table holds the title, artist and URL of music I have on my machine. Several of the single quotes have been dropped from the track title (for example, the don't is stored as dont) and I'm attempting to replace them. I just cannot get this query to affect any rows:
UPDATE Songs
SET Title = REPLACE (Title, 'dont', 'don\'t')
No love. Isn't this correct syntax? It tells me that 0 rows were updated.
If it helps, I'm running version 5.5.27. I know there are a couple hundred rows with improper donts in there... I'm about to dump the results into Notepad and do a find/replace on don't and just run an update statement that way, but it's kinda hacky. Any ideas friends?
A couple of sample rows:
"51","Dont Stay Home","311","311 Greatest Hits","Rap","E:\Music\311\311GreatestHits\dontstayhome.mp3"
"229","Dont Turn Around","Ace Of Base","The Very Best Of","Dance","E:\Music\AceofBase\VeryBestOf\03-ace_of_base-dont_turn_around.mp3"
The Fields in order are id, title, artist, album, genre, path
You have to do it like this
UPDATE Songs
SET Title = REPLACE(Title, 'Dont', 'Don\'t');
^ ^
The reason for that is
REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str
replaced by the string to_str. REPLACE() performs a case-sensitive
match when searching for from_str.
If you want to replace either case you can do
UPDATE Songs
SET Title = REPLACE(REPLACE(Title, 'Dont', 'Don\'t'), 'dont', 'don\'t')
WHERE Title LIKE '%dont%' -- it makes sense to limit update to only those rows that have dont in it no matter case
Here is SQLFiddle demo
The first thought is that there might be some invisible characters. What does the following return?
select *
from songs
where title like '%dont%';
EDIT:
I didn't notice this at first. The problem is the space after the function name. Try this:
UPDATE Songs
SET Title = REPLACE(Title, 'dont', 'don\'t');
This is explained in the documentation:
Note
By default, there must be no whitespace between a function name and
the parenthesis following it. This helps the MySQL parser distinguish
between function calls and references to tables or columns that happen
to have the same name as a function. However, spaces around function
arguments are permitted.
EDIT II:
I don't know if the collation has an effect. But you can also try using double quotes as the string delimiter:
UPDATE Songs
SET Title = REPLACE(Title, 'dont', "don't");