作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 sql 新手,我想要那个,我有一个表 Customer
我在其中添加了一个新字段 Region
现在我想做一些事情
SQL 的改变。我想获取所有记录 Customer
并检查每个记录
// This check will perfome for every customer let suppose we have our first customer
if currentcustomer.Country = 'US'
{
UPDATE currentcustomer SET currentcustomer.Region=currentcustomer.id
}
else
{
UPDATE currentcustomer SET currentcustomer.Region=currentcustomer.ZIP
}
这是我想做的,但我不知道如何对所有客户执行此操作以及如何在 sql 中执行此操作。请帮帮我谢谢!!!
最佳答案
UPDATE Customer SET Region='AA'
WHERE Country = 'US'
UPDATE Customer SET Region='ZZ'
WHERE Country <> 'US'
避免在 SQL 中循环 - 它不是为循环而设计的。
尝试用基于集合的术语来表述您的要求(我需要更新所有国家为 US
的客户,使其拥有区域 AA
,所有其他客户都更新为区域 ZZ
)。
更新:
看到您的编辑,并假设它是合理的(兼容的数据类型和有意义的数据):
UPDATE Customer SET Region= id
WHERE Country = 'US'
UPDATE Customer SET Region= Zip
WHERE Country <> 'US'
关于sql - 如何循环遍历sql中表的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10719137/
我是一名优秀的程序员,十分优秀!