当前位置:网站首页>Data modification modification
Data modification modification
2022-07-24 13:45:00 【Hua Weiyun】
12.2 Data update
MySQL Support to update the data in the data table , Use UPDATE Statement to update the data records in the data table . All records in the data table can be updated , You can also specify update conditions to update specific records in the data table .
The syntax format of the updated data is as follows :
UPDATE table_nameSET column1=value1, column2=value2, … , column=valuen[WHERE condition]The syntax format is described as follows :
·table_name: The name of the table that needs to update the data .
·column1,column2,…,columnn: Field name to be updated .
·value1,value2,…,valuen: The updated value of the field .
·condition: The conditions and restrictions that the updated records need to meet .
among ,WHERE Conditional statements can be omitted , When omitting WHERE Conditional statement , Update all data in the data table .
12.2.1 Update all records in the data table
Update all records in the data table , Only need to UPDATE Of the statement WHERE Omit the condition .
for example , take t_goods In the data sheet t_upper_time The fields are uniformly updated to “2020-12-12 00:00:00”.
mysql> UPDATE t_goods SET t_upper_time = '2020-12-12 00:00:00';Query OK, 12 rows affected (0.11 sec)Rows matched: 12 Changed: 12 Warnings: 0SQL Statement executed successfully , Next look at t_goods Data in the data table .
mysql> SELECT * FROM t_goods;+----+---------------+-----------------+------------+---------+---------+---------------------+| id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time |+----+---------------+-----------------+------------+---------+---------+---------------------+| 1 | 1 | Women's wear / Women's Boutique | T T-shirt | 39.90 | 1000 | 2020-12-12 00:00:00 || 2 | 1 | Women's wear / Women's Boutique | dress | 79.90 | 2500 | 2020-12-12 00:00:00 || 3 | 1 | Women's wear / Women's Boutique | fleece | 79.90 | 1500 | 2020-12-12 00:00:00 || 4 | 1 | Women's wear / Women's Boutique | A pair of jeans | 0.00 | 0 | 2020-12-12 00:00:00 || 5 | 1 | Women's wear / Women's Boutique | Pleated skirt | 29.90 | 500 | 2020-12-12 00:00:00 || 6 | 1 | Women's wear / Women's Boutique | Woolen coat | 399.90 | 1200 | 2020-12-12 00:00:00 || 7 | 2 | Outdoor sports | Bicycle | 399.90 | 1000 | 2020-12-12 00:00:00 || 8 | 2 | Outdoor sports | Mountain Bike | 1399.90 | 2500 | 2020-12-12 00:00:00 || 9 | 2 | Outdoor sports | Alpenstocks | 59.90 | 1500 | 2020-12-12 00:00:00 || 10 | 2 | Outdoor sports | Riding equipment | 399.90 | 3500 | 2020-12-12 00:00:00 || 11 | 2 | Outdoor sports | Sport coat | 799.90 | 500 | 2020-12-12 00:00:00 || 12 | 2 | Outdoor sports | Skate | 499.90 | 1200 | 2020-12-12 00:00:00 |+----+---------------+-----------------+------------+---------+---------+---------------------+12 rows in set (0.00 sec)t_goods In the data sheet t_upper_time The data of the field is uniformly modified to “2020-12-12 00:00:00”, It indicates that the data is updated successfully .
12.2.2 Update specific data rows in the table
MySQL Support to update specific data rows in the table , here , Need to add WHERE Conditions restrict the updated data records .
for example , take t_goods In the data table id by 2 The commodity name of the data record of is modified to “ Avocado Green Dress ”. First , see t_goods In the data table id by 2 The data of .
mysql> SELECT * FROM t_goods WHERE id = 2;+----+---------------+---------------------+-----------+---------+---------+-------------------+| id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time |+----+---------------+---------------------+-----------+---------+---------+-------------------+| 2 | 1 | Women's wear / Women's Boutique | dress | 79.90 | 2500 |2020-12-12 00:00:00|+----+---------------+---------------------+-----------+---------+---------+-------------------+1 row in set (0.00 sec)You can see , Before modifying the data id by 2 The recorded commodity name of is “ dress ”. Execute the SQL sentence .
mysql> UPDATE t_goods SET t_name = ' Avocado Green Dress ' WHERE id = 2;Query OK, 1 row affected (0.38 sec)Rows matched: 1 Changed: 1 Warnings: 0SQL Statement executed successfully , Look again t_goods In the data table id by 2 Data record of .
mysql> SELECT * FROM t_goods WHERE id = 2; +----+---------------+---------------+-----------------+---------+---------+-------------------+| id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time |+----+---------------+---------------+-----------------+---------+---------+-------------------+| 2 | 1 | Women's wear / Women's Boutique | Avocado Green Dress | 79.90 |2500 |2020-12-12 00:00:00|+----+---------------+-------- ------+-----------------+---------+---------+-------------------+1 row in set (0.00 sec)The data has been modified to “ Avocado Green Dress ”, It indicates that the data is modified successfully .
12.2.3 Update the data in a certain range
MySQL Support to update data in a certain range , Can pass BETWEEN…AND Statements or “>”“>=”“<”“<=”“<>”“!=” Equal operator , perhaps LIKE、IN、NOT IN And so on .
1. Use BETWEEN…AND Statement update data
for example , take t_goods In the data table id by 1~6 Of data records t_upper_time The value of the field is updated to “2020-11-11 00:00:00”.
mysql> UPDATE t_goods SET t_upper_time = '2020-11-11 00:00:00' WHERE id BETWEEN 1 AND 6;Query OK, 6 rows affected (0.00 sec)Rows matched: 6 Changed: 6 Warnings: 0SQL Statement executed successfully , Next look at t_goods Records in the data table .
mysql> SELECT * FROM t_goods;+----+---------------+---------------+----------------+---------+---------+--------------------+| id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time |+----+---------------+---------------+----------------+---------+---------+--------------------+| 1 | 1 | Women's wear / Women's Boutique | T T-shirt | 39.90 | 1000 | 2020-11-11 00:00:00|| 2 | 1 | Women's wear / Women's Boutique | Avocado Green Dress | 79.90 | 2500 | 2020-11-11 00:00:00|| 3 | 1 | Women's wear / Women's Boutique | fleece | 79.90 | 1500 | 2020-11-11 00:00:00|| 4 | 1 | Women's wear / Women's Boutique | A pair of jeans | 0.00 | 0 | 2020-11-11 00:00:00|| 5 | 1 | Women's wear / Women's Boutique | Pleated skirt | 29.90 | 500 | 2020-11-11 00:00:00|| 6 | 1 | Women's wear / Women's Boutique | Woolen coat | 399.90 | 1200 | 2020-11-11 00:00:00|| 7 | 2 | Outdoor sports | Bicycle | 399.90 | 1000 | 2020-12-12 00:00:00|| 8 | 2 | Outdoor sports | Mountain Bike | 1399.90 | 2500 | 2020-12-12 00:00:00|| 9 | 2 | Outdoor sports | Alpenstocks | 59.90 | 1500 | 2020-12-12 00:00:00|| 10 | 2 | Outdoor sports | Riding equipment | 399.90 | 3500 | 2020-12-12 00:00:00|| 11 | 2 | Outdoor sports | Sport coat | 799.90 | 500 | 2020-12-12 00:00:00|| 12 | 2 | Outdoor sports | Skate | 499.90 | 1200 | 2020-12-12 00:00:00|+----+---------------+---------------+----------------+---------+---------+--------------------+12 rows in set (0.01 sec)id by 1~6 Of data records t_upper_time The data of the field was successfully modified to “2020-11-11 00:00:00”.
2. Update data with operators
for example , The commodity price is greater than or equal to 399.90 element , Less than or equal to 799.90 The listing time of RMB products is modified to “2020-06-18 00:00:00”.
mysql> UPDATE t_goods SET -> t_upper_time = '2020-06-18 00:00:00' -> WHERE -> t_price >= 399.90 AND t_price <= 799.90;Query OK, 5 rows affected (0.00 sec)Rows matched: 5 Changed: 5 Warnings: 0SQL Statement executed successfully , see t_goods Data in the data table .
mysql> SELECT * FROM t_goods;+----+---------------+---------------+----------------+---------+---------+--------------------+| id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time |+----+---------------+---------------+----------------+---------+---------+--------------------+| 1 | 1 | Women's wear / Women's Boutique | T T-shirt | 39.90 | 1000 | 2020-11-11 0:00:00 || 2 | 1 | Women's wear / Women's Boutique | Avocado Green Dress | 79.90 | 2500 | 2020-11-11 00:00:00 || 3 | 1 | Women's wear / Women's Boutique | fleece | 79.90 | 1500 | 2020-11-11 00:00:00 || 4 | 1 | Women's wear / Women's Boutique | A pair of jeans | 0.00 | 0 | 2020-11-11 00:00:00 || 5 | 1 | Women's wear / Women's Boutique | Pleated skirt | 29.90 | 500 | 2020-11-11 00:00:00 || 6 | 1 | Women's wear / Women's Boutique | Woolen coat | 399.90 | 1200 | 2020-06-18 00:00:00 || 7 | 2 | Outdoor sports | Bicycle | 399.90 | 1000 | 2020-06-18 00:00:00 || 8 | 2 | Outdoor sports | Mountain Bike | 1399.90 | 2500 | 2020-12-12 00:00:00 || 9 | 2 | Outdoor sports | Alpenstocks | 59.90 | 1500 | 2020-12-12 00:00:00 || 10 | 2 | Outdoor sports | Riding equipment | 399.90 | 3500 | 2020-06-18 00:00:00 || 11 | 2 | Outdoor sports | Sport coat | 799.90 | 500 | 2020-06-18 00:00:00 || 12 | 2 | Outdoor sports | Skate | 499.90 | 1200 | 2020-06-18 00:00:00 |+----+---------------+---------------+----------------+---------+---------+--------------------+12 rows in set (0.00 sec)The price in 399.90~799.90 The listing time of the product has been modified to “2020-06-18 00:00:00”, It indicates that the data is modified successfully .
Be careful : When updating data , Other operators are used in the same way , I won't repeat .
3. Use LIKE Statement update data
for example , take t_goods The commodity name in the data sheet contains “ cattle ” The shelf time of the goods of the word is modified to “2020-03-08 00:00:00”.
mysql> UPDATE t_goods SET -> t_upper_time = '2020-03-08 00:00:00' -> WHERE t_name LIKE '% cattle %';Query OK, 2 rows affected (0.00 sec)Rows matched: 2 Changed: 2 Warnings: 0SQL Statement executed successfully , see t_goods Data in the data table .
mysql> SELECT * FROM t_goods;+----+---------------+---------------+---------------+---------+---------+---------------------+| id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time |+----+---------------+---------------+---------------+---------+---------+---------------------+| 1 | 1 | Women's wear / Women's Boutique | T T-shirt | 39.90 | 1000 | 2020-11-11 00:00:00 || 2 | 1 | Women's wear / Women's Boutique | Avocado Green Dress | 79.90 | 2500 | 2020-03-08 00:00:00 || 3 | 1 | Women's wear / Women's Boutique | fleece | 79.90 | 1500 | 2020-11-11 00:00:00 || 4 | 1 | Women's wear / Women's Boutique | Cowboy | 0.00 | 0 | 2020-03-08 00:00:00 || 5 | 1 | Women's wear / Women's Boutique | Pleated skirt | 29.90 | 500 | 2020-11-11 00:00:00 || 6 | 1 | Women's wear / Women's Boutique | Woolen coat | 399.90 | 1200 | 2020-06-18 00:00:00 || 7 | 2 | Outdoor sports | Bicycle | 399.90 | 1000 | 2020-06-18 00:00:00 || 8 | 2 | Outdoor sports | Mountain Bike | 1399.90 | 2500 | 2020-12-12 00:00:00 || 9 | 2 | Outdoor sports | Alpenstocks | 59.90 | 1500 | 2020-12-12 00:00:00 || 10 | 2 | Outdoor sports | Riding equipment | 399.90 | 3500 | 2020-06-18 00:00:00 || 11 | 2 | Outdoor sports | Sport coat | 799.90 | 500 | 2020-06-18 00:00:00 || 12 | 2 | Outdoor sports | Skate | 499.90 | 1200 | 2020-06-18 00:00:00 |+----+---------------+---------------+---------------+---------+---------+---------------------+12 rows in set (0.00 sec)The trade name is “ Avocado Green Dress ” and “ A pair of jeans ” The listing time of has been modified to “2020-03-08 00:00:00”, It indicates that the data is modified successfully .
4. Use IN Statement update data
for example , take t_goods In the data table id by 7~12 The listing time of the product data of is updated to “2020-10-01 00:00:00”.
mysql> UPDATE t_goods SET -> t_upper_time = '2020-10-01 00:00:00' -> WHERE id IN (7, 8, 9, 10, 11, 12); Query OK, 6 rows affected (0.00 sec)Rows matched: 6 Changed: 6 Warnings: 0SQL Statement executed successfully , see t_goods Data in the data table .
mysql> SELECT * FROM t_goods;+----+---------------+--------------+---------------+---------+---------+---------------------+| id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time |+----+---------------+--------------+---------------+---------+---------+---------------------+| 1 | 1 | Women's wear / Women's Boutique | T T-shirt | 39.90 | 1000 | 2020-11-11 00:00:00 || 2 | 1 | Women's wear / Women's Boutique | Avocado Green Dress | 79.90 | 2500 | 2020-03-08 00:00:00 || 3 | 1 | Women's wear / Women's Boutique | fleece | 79.90 | 1500 | 2020-11-11 00:00:00 || 4 | 1 | Women's wear / Women's Boutique | A pair of jeans | 0.00 | 0 | 2020-03-08 00:00:00 || 5 | 1 | Women's wear / Women's Boutique | Pleated skirt | 29.90 | 500 | 2020-11-11 00:00:00 || 6 | 1 | Women's wear / Women's Boutique | Woolen coat | 399.90 | 1200 | 2020-06-18 00:00:00 || 7 | 2 | Outdoor sports | Bicycle | 399.90 | 1000 | 2020-10-01 00:00:00 || 8 | 2 | Outdoor sports | Mountain Bike | 1399.90 | 2500 | 2020-10-01 00:00:00 || 9 | 2 | Outdoor sports | Alpenstocks | 59.90 | 1500 | 2020-10-01 00:00:00 || 10 | 2 | Outdoor sports | Riding equipment | 399.90 | 3500 | 2020-10-01 00:00:00 || 11 | 2 | Outdoor sports | Sport coat | 799.90 | 500 | 2020-10-01 00:00:00 || 12 | 2 | Outdoor sports | Skate | 499.90 | 1200 | 2020-10-01 00:00:00 |+----+---------------+--------------+---------------+---------+---------+---------------------+12 rows in set (0.00 sec)t_goods In the data table id by 7~12 The listing time of the product data of is modified to “2020-10-01 00:00:00”, It indicates that the data is modified successfully .
Be careful :NOT IN The use of statement update data is the same as IN Same statement , It's just IN Statement is to update the data that the value of a field is included in the value list ,NOT IN Statement is to update the data that the value of a field is not included in the value list , I won't repeat .
12.2.4 Update the data that conforms to the regular expression
MySQL Matching regular expressions in requires the use of keywords REGEXP, stay REGEXP Keyword followed by regular expression rules .
for example , take t_goods The commodity names in the data sheet are marked with “ skirt ” The listing time of the ending product record , It is amended as follows “2020-08-08 00:00:00”.
mysql> UPDATE t_goods SET -> t_upper_time = '2020-08-08 00:00:00' -> WHERE t_name REGEXP ' skirt $';Query OK, 2 rows affected (0.21 sec)Rows matched: 2 Changed: 2 Warnings: 0SQL Statement executed successfully , see t_goods Data in the data table .
mysql> SELECT * FROM t_goods;+----+---------------+--------------+---------------+---------+---------+---------------------+| id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time |+----+---------------+--------------+---------------+---------+---------+---------------------+| 1 | 1 | Women's wear / Women's Boutique | T T-shirt | 39.90 | 1000 | 2020-11-11 00:00:00 || 2 | 1 | Women's wear / Women's Boutique | Avocado Green Dress | 79.90 | 2500 | 2020-08-08 00:00:00 || 3 | 1 | Women's wear / Women's Boutique | fleece | 79.90 | 1500 | 2020-11-11 00:00:00 || 4 | 1 | Women's wear / Women's Boutique | A pair of jeans | 0.00 | 0 | 2020-03-08 00:00:00 || 5 | 1 | Women's wear / Women's Boutique | Pleated skirt | 29.90 | 500 | 2020-08-08 00:00:00 || 6 | 1 | Women's wear / Women's Boutique | Woolen coat | 399.90 | 1200 | 2020-06-18 00:00:00 || 7 | 2 | Outdoor sports | Bicycle | 399.90 | 1000 | 2020-10-01 00:00:00 || 8 | 2 | Outdoor sports | Mountain Bike | 1399.90 | 2500 | 2020-10-01 00:00:00 || 9 | 2 | Outdoor sports | Alpenstocks | 59.90 | 1500 | 2020-10-01 00:00:00 || 10 | 2 | Outdoor sports | Riding equipment | 399.90 | 3500 | 2020-10-01 00:00:00 || 11 | 2 | Outdoor sports | Sport coat | 799.90 | 500 | 2020-10-01 00:00:00 || 12 | 2 | Outdoor sports | Skate | 499.90 | 1200 | 2020-10-01 00:00:00 |+----+---------------+--------------+---------------+---------+---------+---------------------+12 rows in set (0.00 sec)The trade name is “ Avocado Green Dress ” and “ Pleated skirt ” The listing time of the data record of is modified to “2020-08-08 00:00:00”, It indicates that the data is modified successfully .
Be careful : Knowledge of regular expressions , Readers can refer to relevant learning materials , I won't repeat .
边栏推荐
- 网络安全——报错注入
- position: -webkit-sticky; /* for Safari */ position: sticky;
- 使用activiti创建数据库表报错
- 群体知识图谱:分布式知识迁移与联邦式图谱推理
- 2021-07-09
- 网络安全——中间人攻击渗透测试
- Icml2022 | branch reinforcement learning
- Network security - file upload whitelist bypass
- How to verify the domain name after applying for SSL digital certificate?
- Bayesian width learning system based on graph regularization
猜你喜欢

Simple order management system small exercise

From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"

Rhcsa sixth note

Hcip day 13

Overview of multi view learning methods based on canonical correlation analysis

Network security - file upload whitelist bypass

【无标题】

Soft link, hard link

Aggregation measurement of robot swarm intelligence based on group entropy

Network security -- Service Vulnerability scanning and utilization
随机推荐
Wildcard (Pan domain name) SSL certificate
Flink高级特性和新特性(八)
Chapter VI bus
如何在树莓派上搭建运行 WordPress
Easycvr platform security scanning prompt go pprof debugging information leakage solution
Kunyu(坤舆) 安装 详解
The KAP function of epidisplay package in R language calculates the value of kappa statistics (total consistency, expected consistency), analyzes the consistency of the results of multiple scoring obj
R语言ggpubr包的ggarrange函数将多幅图像组合起来、annotate_figure为组合图像添加注释、注解、标注信息、使用left参数在可视化图像左侧添加注解信息(字体颜色、旋转角度等)
rhcsa第六次笔记
NOIP2021 T2 数列
网络安全——Web渗透测试
关于不定方程解的个数的问题
群体知识图谱:分布式知识迁移与联邦式图谱推理
Aike AI frontier promotion (7.24)
Browser failed to get cookies, browser solution
Statistical table of competition time and host school information of 2022 national vocational college skills competition (the second batch)
Difference between code signing certificate and SSL certificate
R语言tidyr包的gather函数将从宽表转化为长表(宽表转化为长表)、第一个参数指定原多个数据列名称生成的新数据列名称、第二个参数指定原表内容值、第三个和第四个参数通过列索引指定不变的列名称列表
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.Y.axis参数指定Y轴分组标签文本的大小
Nessus安全测试工具使用教程