gpt4 book ai didi

sql - 如何循环遍历sql中表的项目列表

转载 作者:搜寻专家 更新时间:2023-10-30 19:58:05 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com