UPDATING partial string from a field mysql - mysql

I have a table with columns like:
emails, country_code etc
Some of the rows contain emails like:
XXXXX#googlemail.com
I want to change about 10,000 records like this in a way that will affect only the "googlemail.com" part of the value and change all of them to "gmail.com".
UPDATE exmple_table SET emails = REPLACE(emails, '%googlemail.com','%gmail.com');
I tried to find and replace but that making me have to type all 10,000 addresses in the query, any solutions?

You can use 'like' operator to filter out the records which contain 'googlemail' and then perform the string replace on them, as shown below:
update table
set SET emails = REPLACE(emails, 'googlemail.com','gmail.com')
where emails like '%googlemail.com%'

Related

Update Column from another column in same table

we need to update our mail addresses in a table, because we have a new domain-part. The local-part remains untouched
In the table is a column for Name, Surname and Mail. (And other columns which are not important).
We want it to look like this in the end:
Name Surname Mail
Test Name Test.Name#newdomain.com
Test2 Name2 Test2.Name2#newdomain.com
But while trying to do so we broke it and now the mail column only shows the new domain. We used the following code:
update table
set mail = Replace('olddomain.com','newdomain.com')
where mail LIKE '%olddomain.com'
So now we need to restore the mail column and add the new domain-part. Any help?
I'm surprised this works. Normally, replace() takes three arguments:
set mail = Replace(mail, 'olddomain.com', 'newdomain.com')
I might suggest that you include the # in the logic as well.
replace() takes three arguments
update table
set mail = Replace(mail,'#olddomain.com','#newdomain.com')
where mail LIKE '%olddomain.com'

select multi url from one column in mysql table

I have a table with "content" column store forum post, there is one or more url in one record of "content" field, I want to get all the url in the “content" column, one url in one row, I use below code
select substr(`content`, locate(`content`,"http://"))
it work for one url in one record, get a list of url like
http://www.google.com
http://www.facebook.com
...
it only get the first url if there are more than one url in the record.
how to fix it?
Another way to look at it is to try:
SELECT GROUP_CONCAT(substr(`content`, locate(`content`,"http://"))) FROM your_table;
which would concatenate all URLs to a single string and carry on from there - maybe you can split it in the code rather than require the DB to do it. Otherwise you can hack on using an auxiliary table of integers 1-n: SQL split comma separated row

Rename/change entries depending on other MySQL columns

I have a database with a lot of entries for images that I want to rename. Let's say all the images have the name Something_X where X is a number, this is in a column called "name" of my database. There are 2 columns in the database that have the original filesize called "original_width" and "original_height". I want to change all the entries in "name" that it still has the original but it adds the original size to it so that "Something_X" becomes "Something_X_widthxheight", is this possible and how would I do this with PHP my admin in a simple way with preferably a SQL code.
UPDATE <yourTableName>
SET name = CONCAT(name,'_',original_width,'x',original_height);
To update only rows that haven't been updated, you can use the below (sqlFiddle)
UPDATE yourTable
SET name = CONCAT(name,'_',original_width,'x',original_height)
WHERE name NOT LIKE CONCAT('%\_',original_width,'x',original_height);

Find column values that are a start string of given string.

I have a database table that contains URLs in a column. I want to show certain data depending on what page the user is on, defaulting to a 'parent' page if not a direct match. How can I find the columns where the value is part of the submitted URL?
Eg. I have www.example.com/foo/bar/baz/here.html; I would expect to see (after sorting on length of column value):
www.example.com/foo/bar/baz/here.html
www.example.com/foo/bar/baz
www.example.com/foo/bar
www.example.com/foo
www.example.com
if all those URLs are in the table of course.
Is there a built in function or would I need to create a procedure? Googling kept getting me to LIKE and REGEXP, which is not what I need. I figured that a single query would be much more efficient than chopping the URL and making multiple queries (the URLs could potentially contain many path components).
Simple turn around the "Like" operator:
SELECT * FROM urls WHERE "www.example.com/foo/bar/baz/here.html" LIKE CONCAT(url, "%");
http://sqlfiddle.com/#!2/ef6ee/1

List of emails, delete rows out of a table

If I have a list of of email accounts in a .txt file, is there a way I can perform a mysql delete statement for each instance of the rows that contain the email account against a table?
We have a newsletter mailing list which around 600 emails are currently invalid, and we want an easier way of getting rid of them besides manually going in one by one to do it.
Use excel to turn the dataset into a comma separated string and then simply:
DELETE FROM YOUR_TABLE
WHERE email IN ('example#aol.com', 'example2#aol.com')
Note that you will need to manually add the single quotes before and after each email address in excel as well.
Assuming you want to use the text file's contents as the source of addresses to delete from the database:
$addreses = file('emails.txt');
foreach($addresses as $address) {
$safe_address = mysql_real_escape_string($address);
$sql = "DELETE FROM yourtable WHERE (email = '$safe_address');";
$result = mysql_query($sql) or die(mysql_error());
}
is the simplest form. You can do some things to optimize the process, such as creating a list of emails in quoted/comma-separated form and using WHERE IN (...) instead, to reduce the number of queries generated/executed.
Note that I'm using PHP as a pseudo-code placeholder, but the basic principle would be the same in pretty much any language.
What about create the delete statement for single email then use Microsoft excel to complete the rest of the emails? by using the concatenate function and matching with the list of the selected emails