In SQL, is it possible update a column except some values?
I have this scenario:
table 'x' column 'y'
and I want to update only some value of开发者_开发技巧 column 'y'.
For example: if 'y' >= 1000 "DON'T UPDATE ONLY THIS VALUE"
The question is: Is it possible update a column except some values?
Thank you in advance
Of course it is possible to update only a column in selected rows. That is why we have the where clause. In the where clause you specify the condition to find those selected rows.
update mytable
set x=NewValue
where y >= 1000 --or any other conditions or adjust your condition
UPDATE x set y=newVal WHERE y>=1000;
Yes, it is possible to only update certain rows in a table using a WHERE clause.
Now, in table W, if you want to update column X when column Z has a certain value and column Y when Z has a different value, you need to have two UPDATE statements with two different WHERE clauses. i.e.:
UPDATE W set X=1 WHERE Z=2
UPDATE W set Y=3 WHERE Z=5
SQL queries usually don't have embedded if-then logic (which is what you seem to be asking for).
精彩评论