MySql Backslashes disappear when data is updated to another table - mysql

I have a table that contains directory paths and the data looks like this:
But when I run an update statement where I join another table to this one and update the to existing rows in the new table, the backslashes disappear like this:
This is the update statement, where
"MIJob.SourceFile"
has the proper text containing the backslashes, and
"MIJobFileLocation.Path_Folder"
is the column being updated and does not have backslashes in its data.
This is the update statement:
UPDATE MIJobFileLocation
INNER JOIN MIJob
ON MIJobFileLocation.MIJobFileLocationGUID = MIJob.MIJobFileLocationGUID_Source
SET
MIJobFileLocation.Path_Folder = MIJob.SourceFile
WHERE MIJob.SourceFile IS NOT NULL
This SQL will run in a stored procedure in MySQL. How can I preserve the backslashes?
I’ve been googling this for hours with no success.
Thank you.

Execute this line before your update statement. Seems like a hack solution, but it works.
SET SESSION sql_mode='NO_BACKSLASH_ESCAPES'
UPDATE sometable
set.....

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;

OPENQUERY SQL Server MYSQL UPDATE

I have to work on a linked server. My goal: Update an entire table in mysql server(version:8.0.21) via OPENQUERY in SQL Server(version 13.0.1742.0). I tried this but it generates an error Row cannot be located for updating. Some values may have been changed since it was last read and this one The rowset was using optimistic concurrency and the value of a column has been changed after the containing row was last fetched or resynchronized.
update linkedTable
set
linkedTable.id_parent=unlinkedTable.IdCat1,
linkedTable.code=unlinkedTable.CodeFamilleFAT,
linkedTable.niveau=unlinkedTable.NiveauCategorieFAT,
linkedTable.langue=unlinkedTable.CodeLangueFAT,
linkedTable.nom=unlinkedTable.LibelleCommercialFAT,
linkedTable.descriptionA=unlinkedTable.DescriptifCom1FAT,
linkedTable.vignette=null,
linkedTable.id_categorie=unlinkedTable.id
from openquery(NAMELINKEDSERVER, 'select id_categorie, id_parent, code, niveau, langue, nom, description as descriptionA, vignette from DatabaseMySQL.Table') as linkedTable
inner join DatabaseSQLserver.dbo.Table as unlinkedTable on unlinkedTable.Id = linkedTable.id_categorie
Then I tried this:
update linkedTable
set
linkedTable.id_parent=unlinkedTable.IdCat1,
linkedTable.code=unlinkedTable.CodeFamilleFAT,
linkedTable.niveau=unlinkedTable.NiveauCategorieFAT,
linkedTable.langue=unlinkedTable.CodeLangueFAT,
linkedTable.nom=unlinkedTable.LibelleCommercialFAT,
linkedTable.descriptionA=unlinkedTable.DescriptifCom1FAT,
linkedTable.vignette=null,
linkedTable.id_categorie=unlinkedTable.id
from openquery(NAMELINKEDSERVER, 'select id_categorie, id_parent, code, niveau, langue, nom, description as descriptionA, vignette from DatabaseMySQL.Table') as linkedTable
inner join DatabaseSQLserver.dbo.Table as unlinkedTable on unlinkedTable.Id = linkedTable.id_categorie
where linkedTable.id_categorie = 1
This work but only one row is updated. So I wrote a stored procedure to update each line but it took too much time.
Can someone explain why my first query didn't work (question1) and how I can reduce the time of my stored procedure (question2)?
I use while loop (count the number of id and update each id).
Thank you in advance.
Kind Regards.
I resolve the problem by checking some option on ODBC Driver in MySQL and reading some forum. I check this box.
enter image description here
This option allows to avoid the errors quoted previously. With this option, i can update multiple values without error on join or other request. Thank you Solarflare and "Another guy" (i lost the name) for correcting me (EDIT A POST). Have nice day both.

Moving data from one mySQL Table field to another Table field where each table has a matching id

I need to move all the data from table FCSTMR field CSTYPE to table customers2 field customers_group_pricing.
The value stored in FCSTMR CS_ENCSUNIQUE matches that stored in customers2 customers_id.
I tried using the following mysql, but it didn't move any data at all.
UPDATE customers2
SET `customers_group_pricing` = (
SELECT `CSTYPE`
FROM FCSTMR
WHERE CS_ENCSUNIQUE = customers2.customers_id);
Where did i go wrong with this? I'm assuming something to do with the WHERE statement.
Have a look this question's last comment. I think it is similar to yours.

mysql update without replacing existing data

I need to update a row in my mysql database, but i don't want to replace the data that is already stored in it. example:
select books from storedb where id='Rick';
result of the query: Books = "example1"
but i need to update that row and add more books.
update storedb set books='example2' where id='Rick';
but it replaces the current data, so i need to do it without replacing current data.
somethink like this: books='example1 -- example2";
so you need a string concatenation?
Try this:
update storedb set books=CONCAT(books,' -- ', 'example2') where id='Rick';

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/')