English 中文(简体)
如何在 sql 表格的项目列表中循环
原标题:How to loop over a list of items of a table in sql

i am new in sql and i want that , i have a table Customer
in which i added a new field Region now i want to do some
alteration in SQL . I want to get all records Customerand check on each

 // 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 和所有其他客户在区域 中都具有区域 /code > )。

更新 :

看到您的编辑, 并假设它是合理的( 兼容的数据类型和有意义的数据) :

UPDATE Customer SET Region= id
WHERE Country =  US 

UPDATE Customer SET Region= Zip
WHERE Country <>  US 
问题回答

暂无回答




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...