MYSQL Update IF a subtring exists in another table Inner Join - mysql

What I'm working on is an email clean up script for our database. In so doing we identified a list of domains that are invalid, broken or no longer around. We do this by identifying the domain name i.e. everything after the # sign.
update url_links
set link_bad=1,
emailCarrier='bad-domain.com'
where contact_email like '%#bad-domain.com';
identifies the provider and sets the field. The problem is we have hundreds of domains that are not valid (the above is just an example)
What I'd like to do is 'inner join' to another table that is called 'emailCarriers. I could write this as a loop in PHP but I wanted to ask the community here if someone had a clever way to do this in MySQL.
The emailCarriers table contains all the bad domain carriers so the query would reference the emailCarriers table and seek to find a match on the substring of the domain name portion (after the # sign) and if its a match then it would perform the update and fill in the 'bad-domain.com' with the corresponding domain from the emailCarriers table.
Thanks!

update url_links ul
join emailCarriers ec on ul.contact_email like concat('%#', ec.domain)
set ul.link_bad=1,
ul.emailCarrier=ec.domain;

You can try something like this:-
UPDATE url_links u JOIN emailCarrier e
ON u.SUBSTRING_INDEX(url_links, '#', 1) = b.provider
SET link_bad = 1

Related

Rails - How to reference model's own column value during update statement?

Is it possible to achieve something like this?
Suppose name and plural_name are fields of Animal's table.
Suppose pluralise_animal is a helper function which takes a string and returns its plural literal.
I cannot loop over the animal records for technical reasons.
This is just an example
Animal.update_all("plural_name = ?", pluralise_animal("I WANT THE ANIMAL NAME HERE, the `name` column's value"))
I want something similar to how you can use functions in MySQL while modifying column values. Is this out-of-scope or possible?
UPDATE animals SET plural_name = CONCAT(name, 's') -- just an example to explain what I mean by referencing a column. I'm aware of the problems in this example.
Thanks in advance
I cannot loop over the animal records for technical reasons.
Sorry, this cannot be done with this restriction.
If your pluralizing helper function is implemented in the client, then you have to fetch data values back to the client, pluralize them, and then post them back to the database.
If you want the UPDATE to run against a set of rows without fetching data values back to the client, then you must implement the pluralization logic in an SQL expression, or a stored function or something.
UPDATE statements run in the database engine. They cannot call functions in the client.
Use a ruby script to generate a SQL script that INSERTS the plural values into a temp table
File.open(filename, 'w') do |file|
file.puts "CREATE TEMPORARY TABLE pluralised_animals(id INT, plural varchar(50));"
file.puts "INSERT INTO pluralised_animals(id, plural) VALUES"
Animal.each.do |animal|
file.puts( "( #{animal.id}, #{pluralise_animal(animal.name)}),"
end
end
Note: replace the trailing comma(,) with a semicolon (;)
Then run the generated SQL script in the database to populate the temp table.
Finally run a SQL update statement in the database that joins the temp table to the main table...
UPDATE animals a
INNER JOIN pluralised_animals pa
ON a.id = pa.id
SET a.plural_name = pa.plural;

updating mysql database email column with id

I'm trying to update a column used for email in my database with the site id for the same row..
I've tried the following:
UPDATE LOCATIONS
SET email ='se".site_id."#myemail.com'
WHERE customer='MyCustomer' AND site_id='5555';
expecting the email column to be se5555 at myemail.com
but that wasn't the case. Should I use CONCAT?
Use concat function of mysql like this:
UPDATE LOCATIONS SET email = concat('se',site_id,'#myemail.com') WHERE
customer='MyCustomer' AND site_id='5555';
You can definitely use concat(). I also like to use replace() for this type of operation:
UPDATE LOCATIONS
SET email = replace('se<site_id>#myemail.com', '<site_id>', site_id)
WHERE customer = 'MyCustomer' AND site_id = '5555';
This is helpful when you have multiple substitutions -- the first argument is a template so it is easier to see what you are doing and to modify.

MySQL WHERE clause not working after CSV import

I have a problem with a MySQL WHERE clause. I think I know what the problem is, just not how to fix it.
I have a database with student timetable information and I'm matching this against a table with student information. The student information has been imported into the database from a CSV (utf-8) file, the other information was just inserted into the database with "normal" INSERT queries.
The WHERE clause is simple and looks like this:
WHERE gpu_timetable.cls_name =
(SELECT cls_name FROM gpu_students WHERE std_number = 123441 LIMIT 1)
Its matching the cls_name (class name) from the timetable against the class name from the students table. Like I said the data is from different sources but looks to be the same. For example when I remove the SELECT query and use this string ('LV6A') the code works.
The collation on both of the fields is *utf8_general_ci*, I also tried TRIM() but no success, the same for replacing the operator = with LIKE.
Did I do something wrong when importing the student information or is there another function similar to TRIM() that can fix this weird problem?
Your simplified query must be:
SELECT * FROM gpu_timetable INNER JOIN gpu_students ON gpu_timetable.cls_name = gpu_students.cls_name WHERE gpu_students.std_number = 123441
Your should always have tablename.fieldname while using JOIN queries.

How do I remove an email domain value and add a new one in a column - mysql

So I have a bunch of users in a column that get refreshed as:
Bill#test.comXYZ
Tom#test.comXYZ
John#test.comXYZ
We refresh the database each week and I need to update these appropriate emails to:
Bill#domain.com
Tom#domain.com
John#domain.com
I figured I can use concat to do the latter, but I am stuck on the former issue. Is there a way to split the values (like split Bill#test.comXYZ into Bill - #test.comXYZ and then remove the #TEXT values?).
Anyways, any help will be much appreciated.
You can use the mySQL replace function, i.e.
UPDATE mytable
set myfield = replace (myfield, '#test.comXYZ', 'domain.com')
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

UPDATE mysql database replace strings

I have in my db strings like www.domain.com and http://www.domain.com. I want to prepend to all entries the http:// but not affect other urls and as a result have this: http://http://www.domain.com
Can this be done with mysql only? I have used REPLACE(field,'www','http://www'), but this replaces also the http://www with http://http://www
Thanks in advance
EDIT
I forgot to mention that in the field there might be entries which don't contain www or http://www and therefore I don't want to alter or maybe there are entries like <p>domain</p> in which CONCAT() prepends the http:// before <p>
Try adding a WHERE clause to your update to only update fields that do not already have 'http://'. Test it out like this
SELECT CONCAT('http://', field) FROM foo WHERE LOCATE('http://', field)=0
and your UPDATE syntax would be:
UPDATE foo SET field=CONCAT('http://',field) WHERE LOCATE('http://', field)=0
I won't worry about performance as this seems like a one-off kind of script. That said, you can couple LEFT and CONCAT to achieve this:
UPDATE mytable
SET mycolumn = CONCAT('http://',mycolumn)
WHERE LEFT(mycolumn,7) <> 'http://'
Do note that I'm not taking CapItaliZation in to account. You may also want to consider sanitizing the information either before adding it to the database, or maybe make a trigger to do it for you.
Search and Replace Query - mysql replace
Here is the SQL query to replace string in your MySQL database table:
UPDATE table_name SET column_name = REPLACE(column_name,'original_string','replace_string')
Here is what I did to change the path URLs in all my previous posts.
UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`,'http://localhost/','https://sureshkamal1.wordpress.com/')