E
E
enchikiben2010-11-09 10:02:02
MySQL
enchikiben, 2010-11-09 10:02:02

Update request with condition?

Good afternoon!
Good people, tell me how in mysql it is possible to write an update request with a condition?
For example, if in field 1 then we replace it with 2, if 2 then we replace it with 1.
The field contains the letter “b” and “empty”, I need to swap, so to speak, the letter b is replaced by "empty", and "empty" is replaced by "b".

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg Matrozov, 2010-11-09
@Mear

UPDATE test SET t = IF(t="a","b","a") WHERE t IN ("a","b");

L
lanabel, 2010-11-09
@lanabel

I would change in three requests through the third value, which is definitely not in this column.
Update table set field='c' where field='';
Update table set field='' where field='b';
Update table set field='b' where field='c';

N
Nicolette, 2010-11-09
@Nicolette

Of course, I am writing in Oracle SQL, not MySQL, but is there really no case or decode type construct that allows you to get different values ​​depending on the argument? Three updates for one action - quite wasteful, no?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question