Changing column content in many rows - mysql

I have a column called code and in every row, the column contains FE. Because I do not want to go through 13,000 records, is there a quick way to replace FE inside 'code' with FEU?
While I appreciate this may be a simple question (or not?), I wasn't sure how to word it in order to find a solution.

You should be able to do like this:
UPDATE <table> SET code = REPLACE(code, 'FE', 'FEU');
If the column really just contains the value FE a simple WHERE clause should be enough:
UPDATE <table> SET code = 'FEU' WHERE code = 'FE';

Maybe something like this?
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'
This will work if your column contains only the string 'FE' and you want to replace it with 'FEU'.

update `table`
set `code` = replace(`code` , "FE","FEU")
where (if there is a where write it here)
should do you

This is probably the quickest way. You can try this:-
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'

Related

how to delete single record from list

I have a record in table column like this (1001,1002,1003,1004,1005)
and I want to delete "1003" from this list. Please help me.
For your own good, don't store data like this. This type of issue is not, by far, the biggest problem you will run into.
This being said, you can solve you issue by using:
UPDATE TABLE
SET COLUMN = REPLACE (COLUMN, ',1003,', ',')
WHERE ID = PK;
even this will work:
update tablename set column=(select
substr(column,1,instr(colname,',',2))||susbtr(column,instr(column,',',3),length(column)-
instr(column,',',3)) from tablename where id=value;

MySQL update to change all instances containing -html to .html

I need to update a bunch of records in my database that store a slug in an article table. I mistakenly set the slugs to end in "-html" rather than ".html" and I need a query that will fix this.
I don't really understand how to use variables, so I'm hoping someone here can help.
Would someone please write for me a SQL query that's something like:
UPDATE table
SET table.slug = '%.html%'
WHERE table.slug LIKE '%-html%'
Obviously, that's not correct, but I don't know the correct way to write it.
Here's a quick-and-dirty example using REPLACE()
UPDATE table
SET slug = REPLACE(slug, '-html', '.html')
WHERE slug LIKE '%-html'
Just be warned that this will replace any occurrence of -html, even if it's not at the end of the string.
A more comprehensive approach might be
UPDATE table
SET slug = CONCAT(TRIM(TRAILING '-html' FROM slug), '.html')
WHERE slug LIKE '%-html'
Mine will only replace the last '-html', and append '.html'by CONCAT:
UPDATE table
SET slug = CONCAT(SUBSTRING(slug, 1, LENGTH(slug) - 5), '.html')
WHERE slug LIKE '%-html'
You have to make use of replace command.
UPDATE Table Tablename
SET MyColumnname = REPLACE(MyColumnname, '-html', '.html')
WHERE MyColumnname LIKE '%-html%'
I'd make use the the TRIM and CONCAT functions:
Something like this:
UPDATE `table` t
SET t.slug = CONCAT(TRIM(TRAILING '-html' FROM t.slug),'.html')
WHERE t.slug LIKE '%-html'
Note that the TRIM(TRAILING '-html' will remove all occurrences of that specified string from the end of the column value, so if I had (for example) a column value of 'foo-html-bar-html-html', that would return 'foo-html-bar'.
I use the CONCAT function to append '.html'.
The WHERE clause guarantees that I will only be modifying rows that have a column value ending in '-html'.
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html

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

DBIx::Class update only one row

I am using DBIx::Class and I would like to only update one row in my table. Currently this is how I do it:
my $session = my_app->model("DB::Session")->find(1);
$session->update({done_yn=>'y',end_time=>\'NOW()'});
It works, but the problem is that when it does find to find the row, it does this whole query:
SELECT me.id, me.project_id, me.user_id, me.start_time, me.end_time, me.notes, me.done_yn FROM sessions me WHERE ( me.id = ? ): '8'
Which seems a bit much when all I want to do is update a row. Is there anyway to update a row without having to pull the whole row out of the database first? Something like this is what I am looking for:
my_app->model("DB::Session")->update({done_yn=>'y',end_time=>\'NOW()'},{id=>$id});
Where $id is the WHERE id=? part of the query. Does anyone know how to do this? Thanks!
You can run update on a restricted resultset which only matches this single row:
my_app->model("DB::Session")->search_rs({ id=> 1 })->update({done_yn=>'y',end_time=>\'NOW()'});
I suggest you use a DateTime->now object instead of literal SQL for updating the end_time column because it uses the apps servers date and time instead of the database servers and makes your schema more compatible with different RDBMSes.
Do you have a check if the row was found to prevent an error in case it wasn't?
You might want to use update_or_create instead.
You could use the "columns" attribute:
my $session = my_app->model("DB::Session")->find(1, {columns => "id"});

Update MySQL Column with WHERE condition

I need to change the information in a column to Degraded, where it is Feature Broken or Degradated. Is this the correct line?
UPDATE tablename
SET column = 'Degraded'
WHERE column = 'Degradated' OR column= 'Feature Broken'
Thanks
You may want to use an IN operator, to avoid the OR:
UPDATE tablename SET columnname = 'Degraded' WHERE columnname IN ('Degradated', 'Feature Broken');
Also, I would suggest running a SELECT first so you are somewhat aware of how many rows will be changed with your command.
Yes, it is.
You could make it more concise (but it is exactly the same otherwise)
...
WHERE column IN ('Degradated','Feature Broken')