I have a bunch of image references in a table like:
data/fabrics/signature/signature1.jpg
data/fabrics/signature/signature2.jpg
And I need to go through the table and add an additional folder to the image path like:
data/group1/fabrics/signature/signature1.jpg
data/group1/fabrics/signature/signature2.jpg
Is there any way to do this with just MySQL queries, or will I need to use PHP to concatenate the strings and then update the database?
You could use a simple replacement function, replacing the beginning of the string with a different beginning. Probably the easiest solution if the replacements are so simple.
UPDATE tbl SET col=REPLACE(col, 'data/fabrics', 'data/group1/fabrics') WHERE xxx;
You might have to run several of these, depending on the specific replacements needed, with different WHERE clauses.
update table1
set link=substring(link,1,charindex('/',link,1))+
'group1'+substring(link,charindex('/',link,1),LEN(link))
where (your condition)
SQL Fiddle
Related
I currently have a column (control_reference) setup as a varchar(120) with the data that looks something like this 15591_8571. The data in this column changes the first number but the second number is always the same. I would like to add something to the middle of this so the varchar would look something like this in the end. Remember I have hundreds of these so I wanted to find a way to change them all.
Current values in control_reference
15591_8571
16772_8571
20541_8571
New values in control_reference
15591_I18n_8571
16772_I18n_8571
20541_I18n_8571
I know I can use a simple UPDATE 'database.table' SET 'control_reference'='15591_I18n_8571' WHERE'id'='some_number'; but I have a lot of them to do.
If all rows in the table need to be updated, try:
UPDATE database.table SET control_reference=REPLACE(control_reference, '_', '_I18n_')
To limit the update to rows that match that pattern, add something like:
WHERE control_reference REGEXP '[[:digit:]]+_[[:digit:]]+'
Create a function or stored procedure with REPLACE command and use it.
Simple Usage of REPLACE sysntax
SELECT REPLACE('15591_8571', '_', '_I18n_') AS NewValue;
Result :
15591_I18n_8571
I work on my site in localhost, I have a field in my database which I store absolute url of some pictures, so all records are like http://localhost/my_project/images/picture.jpg and now in my website this records not work, I should change all records to like this 'http://www.mysite.com/images/picture.jpg', so I found replace command for mysql but this command will replace all part of field:
UPDATE tablename SET tablefield = replace(tablefield, "findstring", "replacestring");
How can I change just http://localhost/ in my records with phpmyadmin ?
First, you should remove the domain name from all of your url's. Simple using /images/picture.jpg will allow it to work on all hosts.
To replace it globally, I would recommend doing a mysqldump and opening the file in a text editor, replacing the strings, and importing it back into the database. That's assuming you don't have any serialized strings (wordpress) in your database.
To simply strip the 'http://localhost' prefix, in order to use host-less absolute paths as recommended in #Rob's answer:
UPDATE tablename
SET tablefield = SUBSTR(tablefield FROM 17)
WHERE tablefield LIKE 'http://localhost/%'
See it on sqlfiddle.
To replace with 'http://www.mysite.com' as asked in the original question:
UPDATE tablename
SET tablefield = CONCAT('http://www.mysite.com', SUBSTR(tablefield FROM 17))
WHERE tablefield LIKE 'http://localhost/%'
See it on sqlfiddle.
Of course, neither of these answers use regular expressions as requested in your question title (although they do use simple pattern matching); to use regular expression pattern replacement, you will need to install and use a UDF such as provided by lib_mysqludf_preg.
I am struggling with this query and want to know if I am wasting my time and need to write a php script or is something like the following actually possible?
UPDATE my_table
SET #userid = user_id
AND SET filename('http://pathto/newfilename_'#userid'.jpg')
FROM my_table
WHERE filename
LIKE '%_%' AND filename
LIKE '%jpg'AND filename
NOT LIKE 'http%';
Basically I have 700 odd files that need renaming in the database as they do not match the filenames as I am changing system, they are called in the database.
The format is 2_gfhgfhf.jpg which translates to userid_randomjumble.jpg
But not all files in the database are in this format only about 700 out of thousands. So I want to identify names that contain _ but don't contain http (thats the correct format that I don't want to touch).
I can do that fine but now comes the tricky bit!!
I want to replace that file name userid_randomjumble.jpg with http://pathto/filename_userid.jpg So I want to set the column user_id in that row to a variable and insert it into my new filename.
The above doesn't work for obvious reasons but I am not sure if there is a way round what I'm trying to do. I have no idea if it's possible? Am I wasting my time with this and should I turn to PHP with mysql and stop being lazy? Or is there a way to get this to work?
Yes it is possible without the php. Here is a simple example
SET #a:=0;
SELECT * FROM table WHERE field_name = #a;
Yes you can do it using straightforward SQL:
UPDATE my_table
SET filename = CONCAT('http://pathto/newfilename_', userid, '.jpg')
WHERE filename LIKE '%\_%jpg'
AND filename NOT LIKE 'http%';
Notes:
No need for variables. Any columns of rows being updated may be referenced
In mysql, use CONCAT() to add text values together
With LIKE, an underscore (_) has a special meaning - it means "any single character". If you want to match a literal underscore, you must escape it with a backslash (\)
Your two LIKE predicates may be safely merged into one for a simpler query
I have a table "locales" with a column named "name". The records in name always begin with a number of characters folowed by an underscore (ie "foo_", "bar_"...). The record can have more then one underscore and the pattern before the underscore may be repeated (ie "foo_bar_", "foo_foo_").
How, with a simple query, can I get rid of everything before the first underscore including the first underscore itself?
I know how to do this in PHP, but I cannot understand how to do it in MySQL.
SELECT LOCATE('_', 'foo_bar_') ... will give you the location of the first underscore and SUBSTR('foo_bar_', LOCATE('_', 'foo_bar_')) will give you the substring starting from the first underscore. If you want to get rid of that one, too, increment the locate-value by one.
If you now want to replace the values in the tables itself, you can do this with an update-statement like UPDATE table SET column = SUBSTR(column, LOCATE('_', column)).
select substring('foo_bar_text' from locate('_','foo_bar_text'))
MySQL REGEXs can only match data, they can't do replacements. You'd need to do the replacing client-side in your PHP script, or use standard string operations in MySQL to do the changes.
UPDATE sometable SET somefield=RIGHT(LENGTH(somefield) - LOCATE('_', somefield));
Probably got some off-by-one errors in there, but that's the basic way of going about it.
I found a CSV database of Cities/ZIP/GPS, and when I imported it, it added a " infront of the columns.
alt text http://www.grabup.com/uploads/58754a865eebd94c9aafaf7444b52d15.png?direct
I don't want to go in for 33,000 entries and do this manually, is there a query I can run that will remove the quotes?
i'm not a MySql expert but this should work: (based on my similar experience in Sql Server)
UPDATE table_name SET col_name = REPLACE(col_name, '"', '')
For more info on the REPLACE and other string parsing functions, see here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
in sql server you coud do:
update mytable set state= substring(state,2,29)
change the "29" to whatever the actual length is.
I am sure mysql must have equivalent syntax.
Repeat for each field, it looks like there is only a handful of them.
As an alternative you could filter the original csv document - isn't that easier?