在单个查询中更新2列. [英] Update 2 Columns in s single query.

查看:90
本文介绍了在单个查询中更新2列.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是维卡什.

假设我有一个数据表,如下所示:

EmpID等级
1 A
2 B
3 B
4 A
5 B
6 A
7 A
8 A

现在,我想在一个更新查询中将所有A设为B,将所有B设为A.

该怎么办呢?

请帮忙.

在此先感谢.

hello, this is vikash.

Say I have a table with data as follows :

EmpID Grade
1 A
2 B
3 B
4 A
5 B
6 A
7 A
8 A

now i want to make all A as B and all B as A in a single update query.

How can this be done.

Please help.

Thanks in Advance.

推荐答案

使用CASE语句(可能还使用WHERE子句仅过滤a和b).

Use a CASE statement (and possibly a WHERE clause to filter for only a''s and b''s).

UPDATE [grades]
   SET [grade] = case when [grade] = 'A' then 'B'
                    else 'A'
                    end



干杯.



Cheers.


可以使用case块来完成.请参考以下示例:

This can be done using case block. Refer to this example:

UPDATE titles
       SET price =
                 CASE
                   WHEN (price < 5.0 AND ytd_sales > 999.99)
                                   THEN price * 1.25
                   WHEN (price < 5.0 AND ytd_sales < 1000.00)
                                   THEN price * 1.15
                   WHEN (price > 4.99 AND ytd_sales > 999.99)
                                   THEN price * 1.2
                   ELSE price
                 END




您的情况要简单得多.




your case is much simpler.


这篇关于在单个查询中更新2列.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆