MySQL - UPDATE part of a string in a column - mysql

I am trying to replace some text in a column - text that I have confirmed exists. When I run the query:
SELECT Body FROM dbname.tcontent
where body like '%http://local.website.org%'
and display=1
and approved=1;
it returns 359 records. So, I was expecting 359 records to be changed when I ran this query:
update dbname.tcontent
set Body = replace(Body, 'http://local.website.org', '/foldername')
where instr(Body, 'http://local.website.org') > 0
and display=1
and approved=1;
However, the query found 359 matches, but updated no columns. The message reads: "0 row(s) affected Rows matched: 359 Changed: 0 Warnings: 0. I'm pretty sure I've got the SQL correct as I've confirmed it from at least three sources. I have read that the 359 "matches" are based on records matching the WHERE clause, and so I believe the problem is with that second line:
set Body = replace(Body, 'http://local.website.org', '/foldername')
even though the SQL is correct. I've unchecked "Safe Updates" in my preferences just to be sure, and still, no rows are being updated.
I still think that maybe the slashes and/or dots in the text I am replacing are tripping up the replace method despite an example of doing what I want to do here.
Thanks in advance for any assistance!

For whatever reason, I had to break it down into two statements. First Update statement replaced http:// with nothing, and the second replaced the remainder of the URL:
update database.tcontent set Body = replace(Body, 'http://', '') where Body like '%http://local.website.org%';
update database.tcontent set Body = replace(Body, 'local.website.org', '/foldername') where Body like '%local.website.org%';

Try this query instead:
UPDATE `dbname`.`tcontent`
SET `body`= REPLACE(`body`, 'FindThisText', 'ReplaceWithThisText')
WHERE `body` like '%FindThisText%' AND `display`=1 and `approved`=1;
Always backup your table before running such UPDATE queries.

Does this not work?
Set Body = 'ReplaceWithThisText' ?

Related

Unable to process a code with special character

UPDATE `ps_product_lang`
SET
`description` = REPLACE(`description`, '<li><p>', '<li>')
Above code is not processing and says 0 Rows affected, however, the following code is working with some rows affected:
UPDATE `ps_product_lang`
SET
`description` = REPLACE(`description`, 'lip', 'li')
How do I process the previous code? I want to replace exactly <li><p>
You're question is similar to this. You may take a look at it:
Update a column value, replacing part of a string
I think you may want to add the LIKE and WHERE clause in your UPDATE query. Something like this: UPDATE yourtable
SET url = REPLACE(url, 'http://domain1.com/images/', 'http://domain2.com/otherfolder/')
WHERE url LIKE ('http://domain1.com/images/%');
Please ensure the description value doesn't be encoded by any encoding.
Or you can try to replace < with < and replace > with >
Below query working fine for me.
UPDATE s_demo SET description = REPLACE(description,'<li><p>','<b>222<b>') WHERE id = 1
I think one thing need to check description field Collation type. In my case i used Collation = utf8_general_ci
I hope, this is to help you

Update data using IF in mysql and define 'dont do anything' when if false

Im trying to use IF query in update some entire row in my mysql table. As known IF query have 3 parameters. Part 1 of what you looking for, part 2 if its found / true and part 3 if its not found / false.
I just want it to be update when its true and doing nothing if false. How to define 'dont do anything' so the false part wont be update into anything else? Sorry for my bad english. And thanks for your help.
You can reassign the same value of column in FALSE part to do nothing.
Try this:
UPDATE tableA SET COL1 = IF(a=b, c, COL1)
You're confusing an update with a condition with a conditional update. For example the following is an update with a condition:
UPDATE x SET y=IF(a=b, c, d)
This is a conditional update:
UPDATE x SET y=c WHERE a=b
The first version is applied to all rows, the second to all matching rows.
Use a WHERE clause to limit what rows your UPDATE is applied to.

SQL Update returning zero rows

UPDATE starfsfolk
SET starfsfolk.stada=2
WHERE starfsfolk.deild LIKE '%Hugbúnaðardeild%';
UPDATE starfsfolk
SET starfsfolk.stada=3
WHERE starfsfolk.deild LIKE '%Markaðsdeild%'
is prescisely the code i'm using.
i've tried various different versions of it(like = "Markaðsdeild" or LIKE "Markaðsdeild")
most of which work if i'm using select, but i needed to use update and its not working for some reason.
This WHERE command works on select but
it returns zero rows if i'm using the update command. What am i doing wrong?
Edit:
Just to clarify, stada is set to 1 in all cases before and after the update command. it hasn't changed from 1 to 2 and 3 like i wanted it to.
Edit2: heres a screenshot of the database, i could also give you the create commands.
Edit3: Stada is bit, not int, i found out, not sure what that changes tho.
Solution: Solved myself, since bit is just 1 and 0, the error was in the creation of the table so i remade it with stada as int and now the code is working.
If the value of stada is not changed by the query then zero rows will be returned because nothing was updated.
The character sets of the server and of the client are different.
http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html

How do i get this IF statement to work in MYSQL (it's in a stored procedure)?

I can't find an example anywhere that doesn't give me syntax errors
My code is
BEGIN
UPDATE Room_Descriptions
IF(STRCMP(Bed_Type,'King')) THEN SET Max_People = Number_Beds * 3
END
Basically it's supposed to go through the table and look at the column "Bed_Type", if it finds the word "King" it'll multiply that row's Number_Beds by 3 and set that in the column Max_People
I was hoping to eventually get some if elses going on but I'll settle for just one if that works.
UPDATE Room_Descriptions SET Max_People = Number_Beds*3 WHERE STRCMP(Bed_Type,'King')
All it takes is a conditional UPDATE.

MySQL add prefix to field table-wide

Basically I just decided to switch my primary ID to a "source" field, as I will be importing stuff from multiple sources. Now I'd like to make it clear where things come from, as such I'd like to add a prefix to it, as to be portalname:formerID. I've tried
UPDATE pics SET source='nk:'+source WHERE 1=1
UPDATE pics SET source='nk:'+source WHERE faces > 0 (matches all records)
but every time phpMyAdmin returns 0 row(s) affected. ( Query took 0.0056 sec )
Any idea?
Use CONCAT() ( http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat ) to concatinate strings, not "+".
you may try to omit the where clause altogether.
UPDATE pics SET source= concat('nk:',source )
or better yet, add a new column 'portal_name' and populate that seperately.