Update MySQL Column with WHERE condition - mysql

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')

Related

Changing column content in many rows

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'

Update MySQL without specifying column names

I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.
If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.
You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html
No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.
Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html
I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.

MySQL set value using text and another field

Okay, not quite sure how to explain it but I'll try my best...
I want to run a MySQL query to set names to 'Unmanaged Team [id]'
My thoughts were initially to use the following (but it doesn't work as hoped).
mysql_query("UPDATE `table` SET `field`='Unmanaged Team'+`id`");
I've tried some variations of that, but I can't quite seems to figure it out, and I don't want to do them one by one, as that would just use far too many queries for something I'm sure can be done in 1.
Many thanks for taking some time to help me out :)
You can use concat:
update `table` set `field` = concat('Unmanaged Team', id);
concat_ws (concat with separator) is also useful if you want whitespace (or some other separator).
Use
SET field = CONCAT('something ',somefield)

UPDATE mysql database replace strings

I have in my db strings like www.domain.com and http://www.domain.com. I want to prepend to all entries the http:// but not affect other urls and as a result have this: http://http://www.domain.com
Can this be done with mysql only? I have used REPLACE(field,'www','http://www'), but this replaces also the http://www with http://http://www
Thanks in advance
EDIT
I forgot to mention that in the field there might be entries which don't contain www or http://www and therefore I don't want to alter or maybe there are entries like <p>domain</p> in which CONCAT() prepends the http:// before <p>
Try adding a WHERE clause to your update to only update fields that do not already have 'http://'. Test it out like this
SELECT CONCAT('http://', field) FROM foo WHERE LOCATE('http://', field)=0
and your UPDATE syntax would be:
UPDATE foo SET field=CONCAT('http://',field) WHERE LOCATE('http://', field)=0
I won't worry about performance as this seems like a one-off kind of script. That said, you can couple LEFT and CONCAT to achieve this:
UPDATE mytable
SET mycolumn = CONCAT('http://',mycolumn)
WHERE LEFT(mycolumn,7) <> 'http://'
Do note that I'm not taking CapItaliZation in to account. You may also want to consider sanitizing the information either before adding it to the database, or maybe make a trigger to do it for you.
Search and Replace Query - mysql replace
Here is the SQL query to replace string in your MySQL database table:
UPDATE table_name SET column_name = REPLACE(column_name,'original_string','replace_string')
Here is what I did to change the path URLs in all my previous posts.
UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`,'http://localhost/','https://sureshkamal1.wordpress.com/')

MYSQL Concat not working

UPDATE profile SET
favourties=CONCAT(favourties,"123")
WHERE id=1
i want to append 123 in favourties but if default value of favourties is set to NULL then this query will not work. What will be query if favourties is set to NULL and then append 123 with it
UPDATE profile SET favourties=CONCAT(IFNULL(favourties, ''),"123") WHERE id=1
Wrap the field around with the COALESCE function:
UPDATE profile
SET favourties = CONCAT(COALESCE(favourties, ''),"123")
WHERE id=1
You probably can't concatenate something to NULL. Maybe you can use coalesce?
UPDATE profile SET favourties=CONCAT(COALESCE(favourites,""),"123") WHERE id=1
see: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
You can use ifnull like #zerkms says, but that is not in the SQL standard. It is a tiny tad faster though. Read up on it at this link: http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/performance-isnull-vs-coalesce.aspx
You can also use CONCAT_WS() (docs) which deals with NULLs as you would expect: converts them to an empty string:
UPDATE profile SET favourties = CONCAT_WS('', favourties, "123") WHERE id = 1;
I personally actually only use CONCAT_WS() now because having to worry about NULLs annoys me. I rarely use NULLs so I don't have to worry about it, but just incase. It's just an annoying thing to figure out why you you're ending up with an empty string when it just doesn't seem to make sense.
In PHP, I use:
SET `trans`=concat('$var', trans)
to add to a string already in the trans column.
It wouldn't work on column named group without using back ticked group inside the brackets as well, whereas with trans, back ticks were not needed.