I'm needing to do a very important database string correction on 192 rows and am wondering if this is correct syntax:
UPDATE `DATABASE_NAME`.`TABLE_NAME` SET `FIELD_NAME` = REPLACE(`FIELD_NAME`,`REPLACE_THIS_STRING`,`WITH_THIS_STRING`);
Thanks in advance!
The best way to find out is to write it as a SELECT statement first to "preview" the results.
SELECT field_name As before
, Replace(field_name, 'replace this string', 'with this string') As after
FROM table_name
Optional WHERE clause (to only affect the rows that contain our replacement string):
...
WHERE field_name LIKE '%replace this string%'
Well, i would write it like this
UPDATE `DATABASE_NAME`.`TABLE_NAME` SET `FIELD_NAME` =
REPLACE(`FIELD_NAME`,`REPLACE_THIS_VALUE`,`WITH_THIS_VALUE`);
Does the table only contain these 192 rows? Otherwise you must add a WHERE to the syntax. Or it will update all FIELD_NAME rows.
As #gvee suggest, try to select the rows first and see how the result looks like to ensure the update scope is correct
Related
I'm looking to replace a string in a CMS multi-site database across a common set of tables. Here is the initial query to collect the target tables:
SELECT TABLE_NAME as target_table
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%_content'
...against the results of which I'd like to run the following:
UPDATE target_table
SET title = replace(title, 'SEARCH_STRING', 'REPLACE_STRING')
WHERE title LIKE ('%SEARCH_STRING%');
Thanks in advance for the assist!
For a one off, a workable approach is to use SQL to generate a set of SQL statements.
Assuming that the table_schem and table_name don't contain backtick characters, and if your SEARCH_STRING and REPLACE_STRING don't contain single quotes (or are properly escaped), we could do something like this:
SELECT CONCAT('UPDATE `',t.table_schema,'`.`',t.table_name,'` c'
,' SET c.title = REPLACE(c.title, ''SEARCH_STRING'', ''REPLACE_STRING'')'
,' WHERE c.title LIKE (''%SEARCH_STRING%'') ;') AS `-- stmt`
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.table_name LIKE '%_content'
AND t.table_schema NOT IN ('information_schema','mysql','performance_schema')
ORDER BY t.table_schema, t.table_name
we can save the results from the query into a file. and then submit the SQL statements in the file to the MySQL server.
(I think I would be using INFORMATION_SCHEMA.COLUMNS, with tables containing column named 'title' as well as the table_name matching a pattern, but the approach is the same.
Note that this cannot be accomplished in a single SQL statement; the query to get the list of tables is going to have to be a separate statement, separated from the execution of the actual UPDATE statement(s).
EDIT I just took a look at the answer linked to in the question; that is totally unrelated. There's nothing there that would apply to the problem we are trying to solve here.
Because of the way SQL is processed (parse, syntax check, semantic check, determine execution plan, then execute) ... identifiers (e.g. table names) must be supplied as tokens in the SQL text. Identifiers cannot be supplied as values at execution time. That's why we need separate statements.
I am trying to look for a way to replace all occurrences of a particular pattern in my database across many tables and columns, for this I need to create some way to do this, it does not need to be done by a script, just some SQL code that will do this.
For example, I want to replace all occurrences of 'v2' with 'www' but have no idea how to do this.
I am not looking for a tutorial, just a bit of guidance on what to do and how to script the SQL needed.
How do I go about doing this?
Just to guide you in a direction. You can use the replace function:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%';
In an earlier post there is more information:
How can I use mySQL replace() to replace strings in multiple records?
You can use a select CONCAT(...) from information_schema.columns to generate an update query for every table-column combination, where ... is a combination of the strings used in an update query, and column names of the information_schema.columns database.
For example:
select CONCAT("UPDATE ", TABLE_NAME, " SET ", COLUMN_NAME, "=REPLACE(",COLUMN_NAME,"'[string-to-find]'","'[string-that-will-replace-it]'",");") FROM information_schema.columns where table_schema = 'your_db';
COLUMN_NAME and TABLE_NAME are columns in the information_schema.columns table, as documented by MySQL
The above query should make the result set:
UPDATE table1 SET field1 = replace(field1,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table1 SET field2 = replace(field2,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table2 SET field3 = replace(field3,'[string-to-find]','[string-that-will-replace-it]');
...
You could output the results of the SELECT statement to a file, which then becomes a script to execute. Alternatively, if you use phpadmin or any other programming language as an interface, you can cycle through the results set, and execute the value of each row in the results set.
I got the idea from MikeW's answer here, about selecting all rows where a data value exists, and from some other stack overflow answers that I have now lost track of, sadly (sorry to the original writers)
To be honest, I think this question may be a duplicate of this, in addition to this though...
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'Text to search, 'Text to replace it with');
I have a column in a table that contains data like:
column: col_name
row 1: 3451,3547,234,456,6487,2341,7856,546,76
row 2: 4746,234,6757,234,7657,6346,563
row 3: 546,7467,4562,456,234
row 4: 563,3246,5641,4637,234,7854,567,577
I would like to be able to select a record based on whether it has a certain number in it.
E.g. as 234 appears in all rows, it selects them all; 563 appears in 2 and so on.
This does not work:
select col_name from table_name where col_name like '%234%';
which I think may be because I'm confusing how I should be selecting columns and rows, but I'm at a blank for what else could work, help appreciated!
Edit: I think I'm confusing myself and some of the repliers (sorry, folks). I'm not trying to find the column name that matches my query, I'm trying to find data in the column that matches my query, aka data in the rows. Some of the answers seem geared towards an unknown column name, but I know which column I'm using.
This is what I would expect to get if I search for a row with 563 in it:
4746,234,6757,234,7657,6346,563 (row 2)
563,3246,5641,4637,234,7854,567,577 (row 4)
If I search for it using all the values in the query like so:
select col_name from table_name where col_name = '4746,234,6757,234,7657,6346,563';
then it will return the row:
4746,234,6757,234,7657,6346,563 (row 2)
but I only want to be able to search for it using one number, not several, as they represent different things.
You can do this using FIND_IN_SET:
SELECT col_name
FROM table_name
WHERE FIND_IN_SET('563', col_name);
Example: SQLFiddle
Check out the way you have stored your data in the database,accordingly select the pattern which includes comma n spaces as well.
your query is otherwise correct !!
try this
SELECT col_name
FROM table_name
WHERE concat(',',col_name,',') like '%,234,%';
select
col_name
from table_name
WHERE col_name LIKE '234,%'
OR col_name LIKE '%,234'
OR col_name LIKE '%,234,%'
OR col_name = '234'
Dont worry about rows. It will search through the rows you only have to provide the column name.
try this one
SELECT col_name FROM table_name WHERE ','+col_name+',' like
'%,234,%';
I have a column in my table named title_id with some title ids. Each title id is stored like
title_1
title_2
title_3
where 1, 2, 3 are the actual ids of the titles.
I want to remove the " title_ " text from all the records. I have around 5000 records so I can't edit them manually.
How can I do it with a query.
Thanks in advance
Update table_name set `title_id` = REPLACE(`title_id`,'title_','');
I didnt' tested it . Please check
UPDATE table_name SET title_id = REPLACE(title_id, 'title_','')
Check out the REPLACE function. You can do something like:
UPDATE table
SET title_id = REPLACE(title_id, 'title_', '');
(Ah, and be sure to first test your UPDATE query by running a SELECT query!)
Try something like
update table_name set id=substring(id, from length('title_'))
where id like 'title%';
I didn't test this, because I have no MySQL DB available here. The syntax for the substring function is from the MySQL docs.
I'm trying to find certain text "catid=18" in a string, where each string is different except for this.
I've used this query below before, except it only seems to work if you know the entire string.
update TABLE_NAME
set FIELD_NAME = replace(FIELD_NAME, 'findthis', 'replacewiththis');
Not sure if that is what you want. But it will return 1 if catid=any_num is found and 0 if not:
select 'some_text catid=18 some_text' REGEXP 'catid=[0-9]+'
Maybe you need:
update TABLE_NAME
set FIELD_NAME = 'goodvalue'
WHERE FIELD_NAME = 'badvalue';