当前位置:网站首页>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 .
边栏推荐
- [acm/ two points] two points clear entry-level explanation
- 开放环境下的群智决策:概念、挑战及引领性技术
- R language uses the tablestack function of epidisplay package to make statistical summary tables (descriptive statistics based on the grouping of target variables, hypothesis testing, etc.), set the b
- Icml2022 | branch reinforcement learning
- 论文笔记:Swin-Unet: Unet-like Pure Transformer for MedicalImage Segmentation
- Network security - file upload competitive conditions bypass
- rhce第一次作业
- R语言使用sort函数排序向量数据实战、返回实际排序后的数据(默认升序)
- Network security - use exchange SSRF vulnerabilities in combination with NTLM trunking for penetration testing
- Explain flex layout in detail
猜你喜欢

游戏思考04总结:针对帧、状态、物理同步的总结(之前写的太长,现在简略下)

Outdoor billboards cannot be hung up if you want! Guangzhou urban management department strengthens the safety management of outdoor advertising

2022.7.22 模拟赛

Aike AI frontier promotion (7.24)

Aggregation measurement of robot swarm intelligence based on group entropy

Network security - function bypass injection

软链接、硬链接

JS execution mechanism

Sringboot plugin framework implements pluggable plug-in services

网络安全——Cookie注入
随机推荐
Network security - war backdoor deployment
Simple order management system small exercise
2022年全国职业院校技能大赛赛项比赛时间、承办校信息统计表(第二批)
Network security - file upload penetration test
网络安全——使用Evil Maid物理访问安全漏洞进行渗透
Network security - file upload blacklist bypass
How to configure webrtc protocol for low latency playback on easycvr platform v2.5.0 and above?
Browser type judgment
Flink容错机制(五)
Adjust the array order so that odd numbers precede even numbers
SQL Server 启停作业脚本
申请了SSL数字证书如何进行域名验证?
群体知识图谱:分布式知识迁移与联邦式图谱推理
脑注意力机制启发的群体智能协同避障方法
Detailed tutorial of ettercap
XSS white list
网络安全——Web信息收集
Editor formula
关于不定方程解的个数的问题
Detailed explanation of odoo JS DoAction