mysql update statement path links adjustment - mysql

I got table smile need to update smilepath ( mass update ) im using this
UPDATE `smilie` SET `smiliepath` = 'http://newpathhere.com/smilies/smile.gif' WHERE `smilieid` =1;
ive to issues ,
need to mass update to all paths , i believe i can use where smileid > 1 not quite sure
2nd issue in old path its looks like http://oldpath.com/icons/smile.gif
so i need to replace only " http://oldpath.com/icons " and keep end file
any clue !

You can use mysql REPLACE function to do this:
UPDATE `smilie` SET `smiliepath` = REPLACE(smiliepath,' http://oldpath.com/icons/','http://newpathhere.com/smilies/') WHERE `smilieid` >1;

Related

MySQL Remove part of string after specific character

I do got a database named "MAPPINGS" with about 23.000 rows I need to replace becouse of a mistake. Is there any query to achieve the following:.
UPDATE MAPPINGS SET MAIN = 'firt part' WHERE USERID = '1578' AND MAIN LIKE 'first part >%'
The problem is that "first part" is every time something else. I just need to remove everyhing after the ">" than the MAPPINGS are correct.
Or can this only be done by a PHP script? while select * from mappings where userid = '1578' and then the update query. I hope there will be a query to achieve this.
You can do this in MySQL using substring_index():
UPDATE MAPPINGS
SET MAIN = SUBSTRING_INDEX(MAIN, '>', 1)
WHERE MAIN LIKE '%>%' AND USERID = '1578';

mysql add http:// to all records in website column

I'm almost done transferring/reconstructing a substantial mysql db for new application. Column 'website' shows 'www.example.com'. Because the new application reads it as a hyperlink, I need the column to read 'http://www.example.com'. Is there a way to add the 'http://' in the beginning of each record for that column? Thanks in advance!
You can use the CONCAT function to do that:
UPDATE tbl SET website=CONCAT('http://', website);
If you want to get cleverer and only update columns which don't already have http:// prepended, try
UPDATE tbl SET website=CONCAT('http://', website)
WHERE website NOT LIKE 'http://%';
Update
To prevent update to columns that have no site in them currently use this
UPDATE tbl SET website=CONCAT('http://', website)
WHERE website NOT LIKE 'http://%' AND website<>'';
You can use the concat command. Something like
SELECT CONCAT('http://', website) FROM table
Use concat to generate the new column:
UPDATE table1 SET website = CONCAT("http://", website);

MySql Add onto an exisiting entry in the database

Just wondering is it possible to add onto an exisiting entry in the database without having to do a query to look whats in there?
Ive done it with numbers before like
UPDATE table SET views = views +1
But is there a way to do it with a string?
Thanks
UPDATE table SET views = CONCAT(views,'some text to add on to it')
What you are looking for is CONCAT() function:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
UPDATE table SET field = CONCAT(field, "some str" ) WHERE id = 123;

MySQL REPLACE variable string

I want to use MySQL query to change a link .
the link is like this :
http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere
if I know the add_id number this is easy :
UPDATE table SET name = REPLACE(name, '&add_id=548124', '')
The problem is I have to change 5000 lines and I don't know the add_id number ... so what would be a correct mysql replace() code to remove &add_id=somenumber ??
USE This....
UPDATE table
SET name = CONCAT(SUBSTRING(name , 1,
INSTR(name ,'&add_id') - 1),SUBSTRING(name ,
INSTR(name , '&more'),
LENGTH(name ) - INSTR(name , '&add_id')))
Either you can do it via UDF - SO Answer or you can simply write PHP code which will replace value & update column again in table.
I would create a stored procedure that uses a cursor to iterate over each row that needs updating.
In the procedure I would find the link to replace and then replace them, one by one.
I've made a sqlfiddle to show how you can get the part to replace inside an select.
I think this approach is clean and easy to read but it's possible to write this a update (that will most likely be hard to read).
first to see that it works :
SELECT 'http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere' INTO #link;
SELECT
#link as full_link,
SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)) as remove_part,
REPLACE(#link,SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)),'') as final_link
And now for your UPDATE:
UPDATE table SET name = REPLACE(name,SUBSTR(name,LOCATE('&',name),LOCATE('&',name,LOCATE('&',name)+1)-LOCATE('&',name)),'')
try this with REPLACE
UPDATE Table1
SET name = REPLACE(if(name like '%add_id=%','' , name ),
'&add_id=' , '' )
DEMO HERE

Mysql Update Field Contents

I am currently trying to edit my db named boh. The current table "files" has a field called "path". Inside the path field is an actualpath to files listed in a folder, syntax "F:\xxx\xxx\xxx\filename.xxx". How do I update the field information to replace the "F:\xxx\xxx\xxx" so that just the file name exists?
It depends what you exactly want, if you want to strip constant path you can use:
UPDATE `table` SET `path` = REPLACE(`path`, 'F:\\xxx\\xxx\\xxx', '');
If you want to keep only last part after last \, then following command should do it:
UPDATE `table` SET `path` = SUBSTRING_INDEX(`path`. '\\', -1);
did you read this?
http://dev.mysql.com/doc/refman/5.1/en/replace.html
UPDATE files
SET path = REPLACE(path, 'F:\xxx\xxx\xxx\', '')
WHERE path LIKE = 'F:\xxx\xxx\xxx\%'
It's very easy to ruin your data with this massive updates so make sure you:
Try it first with a SELECT sentence
Backup your data
Assuming 'F:\xxx\xxx\xxx\' is not constant you could try a statement like this one:
UPDATE files SET path = REVERSE(SUBSTR(REVERSE(path), 1, LOCATE(REVERSE(path), '\')));