当前位置:   article > 正文

SQL UPDATE语句和示例查询

sql update的值按条件查询

SQL databases provide structured data storage capabilities. We can store data in tables with columns and rows. Other useful features for SQL databases are update capabilities. We can update SQL database data in different ways and constraints. In this tutorial, we will learn how to update database table data with an SQL UPDATE statement and query a single record, multiple records, or conditional situations with examples.

SQL数据库提供结构化的数据存储功能。 我们可以将数据存储在具有列和行的表中。 SQL数据库的其他有用功能是更新功能。 我们可以采用不同的方式和约束来更新SQL数据库数据。 在本教程中,我们将学习如何使用SQL UPDATE语句更新数据库表数据以及如何通过示例查询单个记录,多个记录或条件情况。

SQL UPDATE语法 (SQL UPDATE Syntax)

SQL Update statement or query has the following syntax with the given values.

SQL Update语句或查询具有以下具有给定值的语法。

  1. update TABLE_NAME
  2. set COLUMN1=VALUE1, COLUMN2=VALUE2, ... , COLUMNN=VALUEN
  3. where CONDITION;
  • TABLE_NAME is the table in which we want to update its records.

    TABLE_NAME是我们要在其中更新其记录的表。

  • COLUMN1 is the column name we want to update.

    COLUMN1是我们要更新的列名。

  • VALUE1 is the value we want to set the value.

    VALUE1是我们要设置的值。

  •  COLUMN and VALUE can be set for multiple times without a problem.

    可以多次设置COLUMNVALUE

  • CONDITION is used to select the record or row to update.

    CONDITION用于选择要更新的记录或行。

数据库表数据示例 (Example of Database Table Data)

During this tutorial, we will use the following database table in order to update it in different ways. We try to make the table simple in order to prevent misunderstanding and decrease complexity. In this table, we have the following columns. The table is named Users.

在本教程中,我们将使用以下数据库表以不同方式对其进行更新。 我们尝试使表格简单化,以防止误解并降低复杂性。 在此表中,我们具有以下列。 该表名为Users

  • ID

    ID
  • Name

    名称
  • City

  • Age

    年龄
  • Country

    国家
IDNameCityAgeCountry
1Joachim LöwBerlin20Germany
2Ahmet Ali BaydanAnkara35Turkey
3Antonio Moreno TaqueríaMéxico40Mexico
4James BondLondon25UK
5Zlatan İbrahimovicLulea60Sweden
ID 名称 年龄国家
1个约阿希姆·洛(JoachimLöw) 柏林 20 德国
2 艾哈迈德·阿里·拜丹(Ahmet Ali Baydan) 安卡拉 35 火鸡
3 安东尼奥·莫雷诺·塔克里亚 墨西哥40 墨西哥
4 占士邦 伦敦25 英国
5 Zlatanİbrahimovic 露蕾亚 60 瑞典

更新表(Update Table)

We will start with a simple update table example. We will only update the single record single value. We will update the age value of the Zlatan İbrahimovic by using its ID. We will set the age of the Zlatan İbrahimovic to the 38.

我们将从一个简单的更新表示例开始。 我们将只更新单个记录单个值。 我们将使用其ID更新Zlatanİbrahimovic的年龄值。 我们将Zlatanİbrahimovic的年龄设定为38岁。

  1. UPDATE Users
  2. SET Age=38
  3. WHERE ID=5;

更新多个记录 (Update Multiple Records)

Update records can be used to update multiple records in a single Update query execution. We have to specify some conditions which will match multiple records on the given table and update the given columns. In this example, we will the Country of the users whose ages are over 30.

更新记录可用于在单个Update查询执行中更新多个记录。 我们必须指定一些条件,这些条件将匹配给定表上的多个记录并更新给定的列。 在此示例中,我们将选择年龄超过30岁的用户所在的国家/地区。

  1. UPDATE Users
  2. SET Country='Turkey'
  3. WHERE Age>30;

更新多列 (Update Multiple Columns)

We can update multiple columns for the given condition. Here we need to specify the column and value pairs by delimiting them with a comma. In this example, we will update the Country and City values of the users whose ages are over 30. We will set Country as Turkey and City as Ankara.

我们可以针对给定条件更新多个列。 在这里,我们需要通过用逗号定界来指定列和值对。 在此示例中,我们将更新年龄超过30岁的用户的“国家/地区”和“城市”值。我们将“国家/地区”设置为“ Turkey ,将“城市”设置为“ Ankara

  1. UPDATE Users
  2. SET Country='Turkey', City='Ankara'
  3. WHERE Age>30;

更新特定列的所有表行/记录 (Update All Table Rows/Records For Specific Column)

In some cases, we may need to update the whole table or all rows in a table for a specific column. In this case, we do not need a condition because we will match all table rows. In this example, we will set all users table records Age value to the 30.

在某些情况下,我们可能需要为特定列更新整个表或表中的所有行。 在这种情况下,我们不需要条件,因为我们将匹配所有表行。 在此示例中,我们将所有用户表记录的Age值设置为30

  1. UPDATE Users
  2. SET Age=30;

根据ID列更新 (Update According To ID Column)

During the application development, we generally write some SQL queries. The update SQL query is one of the most used ones. Almost every application has some tables which have the ID column. We generally use ID values in order to update the table. In this example, we will show how to update according to ID.

在应用程序开发期间,我们通常会编写一些SQL查询。 更新SQL查询是最常用的查询之一。 几乎每个应用程序都有一些带有ID列的表。 我们通常使用ID值来更新表。 在此示例中,我们将展示如何根据ID更新。

  1. UPDATE Users
  2. SET Age=30
  3. WHERE ID=1;

OR we can update records those ID numbers higher than 3 .

或者,我们可以更新记录,这些ID编号大于3。

  1. UPDATE Users
  2. SET Age=30
  3. WHERE ID>1;

使用BETWEEN子句更新 (Update Using BETWEEN Clause)

Another useful scenario for the SQL Update is using with a BETWEEN clause or condition to match records to be updated. We can specify range by using BETWEEN where the record values are between this range will be updated. In this example, we will update the Country to Turkey where Age is between 30 and 40.

SQL Update的另一个有用方案是使用BETWEEN子句或条件来匹配要更新的记录。 我们可以使用BETWEEN来指定范围,其中记录值在此范围之间将被更新。 在此示例中,我们将国家/地区更新为年龄在30到40岁之间的土耳其。

  1. UPDATE Users
  2. SET Country='Turkey'
  3. WHERE Age BETWEEN 30 AND 40;

使用SQL Select从其他表更新表 (Update Table From Other Table with SQL Select)

Up to now we have updated data by providing explicitly and directly in a SQL query. In complex databases and applications, data can be provided from other tables. We can update data by fetching it from other tables by using the UPDATE SQL statement. We will use an external table named Cities where we will update the Country value of the Users table records according to the Cities table.

到目前为止,我们已经通过显式和直接在SQL查询中提供数据来更新数据。 在复杂的数据库和应用程序中,可以从其他表中提供数据。 我们可以通过使用UPDATE SQL语句从其他表中获取数据来更新数据。 我们将使用一个名为Cities的外部表,在该表中,我们将根据Cities表更新Users表记录的Country值。

  1. UPDATE Users
  2. SET Country=(SELECT Country FROM Cities WHERE Cities.City = Users.City)
  3. WHERE (SELECT Country FROM Cities WHERE Cities.City=Users.City);

使用SQL INNER JOIN更新 (Update Using SQL INNER JOIN)

In the previous example, we have updated the Users table from another table named Cities. We can do this by using an INNER JOIN SQL statement. The scenario is the same as the previous example. We will use an external table named Cities where we will update the Country value of the Users table records according to the Cities table.

在前面的示例中,我们从另一个名为Cities的表中更新了Users表。 我们可以使用INNER JOIN SQL语句来做到这一点。 该方案与前面的示例相同。 我们将使用一个名为Cities的外部表,在该表中,我们将根据Cities表更新Users表记录的Country值。

  1. UPDATE Users
  2. SET Users.Country = Cities.Country
  3. FROM Users
  4. INNER JOIN Cities
  5. ON Users.City =Cities.City;
LEARN MORE  How To Install PostgreSQL Server Into Linux, Debian, Ubuntu, CentOS, Mint, Fedora, Redhat?
了解更多如何将PostgreSQL服务器安装到Linux,Debian,Ubuntu,CentOS,Mint,Fedora,Redhat中?

翻译自: https://www.poftut.com/sql-update-statement-and-query-with-examples/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/958034
推荐阅读
相关标签
  

闽ICP备14008679号