当前位置:网站首页>Tutorial on principles and applications of database system (051) -- MySQL query (XIII): using queries in DML statements
Tutorial on principles and applications of database system (051) -- MySQL query (XIII): using queries in DML statements
2022-07-24 00:50:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (051)—— MySQL Inquire about ( 13、 ... and ):DML Use queries in statements
DML Statement can increase the data in the table 、 Delete 、 Change operation .DML Statement with the help of SELECT Inquire about , It can realize some complex operations .
One 、INSER INTO Statements use SELECT Inquire about
When inserting a record , You can put a SELECT The result of the query is inserted into the data table .
The syntax is as follows :
INSERT INTO Table name ( Name 1, Name 2, ...)
SELECT sentence ;
for example : Create data table stu_age, The table structure is as follows :
/* Create Table stu_age ( s_id char(5) primary key, s_name char(20), gender char(1), age int ); */
mysql> Create Table stu_age (
-> s_id char(5) primary key,
-> s_name char(20),
-> gender char(1),
-> age int
-> );
Query OK, 0 rows affected (0.01 sec)
Perform the following query , Insert the query results stu_age In the table :
/* insert into stu_age select s_id,s_name,gender,year(now())-year(birth) from student where gender = ' male '; */
mysql> insert into stu_age
-> select s_id,s_name,gender,year(now())-year(birth)
-> from student where gender = ' male ';
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> select * from stu_age;
+-------+-----------+--------+------+
| s_id | s_name | gender | age |
+-------+-----------+--------+------+
| S2011 | Zhang Xiaogang | male | 23 |
| S2013 | Cao mengde | male | 24 |
| S2022 | Zhou Huajian | male | 23 |
| S2023 | trump | male | 23 |
| S2024 | Obama | male | 22 |
| S2025 | Zhou Jianhua | male | 22 |
| S2026 | Zhang Xueyou | male | 24 |
| S2032 | Vinci | male | 23 |
+-------+-----------+--------+------+
8 rows in set (0.00 sec)
Two 、DELETE FROM Statements use SELECT Inquire about
Can be in DELETE Ordered WHERE Clause , Delete records that meet the given conditions .
The syntax is as follows :
DELETE FROM Table name
WHERE Name | expression Operator (SELECT Inquire about );
for example : There are two tables as follows
mysql> select * from department;
+------+-----------+
| d_id | d_name |
+------+-----------+
| 11 | The sales department |
| 12 | Logistics Department |
| 13 | Production department |
| 14 | R & D department |
+------+-----------+
4 rows in set (0.00 sec)
mysql> select * from employee;
+-------+-----------+--------+------+
| e_id | e_name | salary | d_id |
+-------+-----------+--------+------+
| 11001 | Zhang Yun | 4500 | 11 |
| 11002 | Liu tao | 4800 | 11 |
| 11003 | Faye Wong | 5200 | 11 |
| 12001 | Liu Jing | 6200 | 12 |
| 12002 | Zhang Hong | 5100 | 12 |
| 12003 | Chen Gang | 6800 | 12 |
| 12004 | Li Zhonghua | 4200 | 12 |
| 13001 | Zhou Tao | 7400 | 13 |
| 13002 | Zhang Qiang | 6800 | 13 |
| 13003 | Zhou long | 6250 | 13 |
| 13004 | Zhang Zhongwei | 4520 | 13 |
+-------+-----------+--------+------+
11 rows in set (0.00 sec)
Delete 【 The sales department 】 All employee information for , The order is as follows :
/* delete from employee where d_id = ( select d_id from department where d_name = ' The sales department ' ); */
mysql> delete from employee where d_id = (
-> select d_id from department where d_name = ' The sales department '
-> );
Query OK, 3 rows affected (0.08 sec)
mysql> select * from employee;
+-------+-----------+--------+------+
| e_id | e_name | salary | d_id |
+-------+-----------+--------+------+
| 12001 | Liu Jing | 6200 | 12 |
| 12002 | Zhang Hong | 5100 | 12 |
| 12003 | Chen Gang | 6800 | 12 |
| 12004 | Li Zhonghua | 4200 | 12 |
| 13001 | Zhou Tao | 7400 | 13 |
| 13002 | Zhang Qiang | 6800 | 13 |
| 13003 | Zhou long | 6250 | 13 |
| 13004 | Zhang Zhongwei | 4520 | 13 |
+-------+-----------+--------+------+
8 rows in set (0.00 sec)
3、 ... and 、UPDATE Statements use SELECT Inquire about
UPDATE Table name
SET Name = expression
WHERE Name | expression Operator (SELECT Inquire about );
for example : There are two tables as follows
mysql> select * from department;
+------+-----------+
| d_id | d_name |
+------+-----------+
| 11 | The sales department |
| 12 | Logistics Department |
| 13 | Production department |
| 14 | R & D department |
+------+-----------+
4 rows in set (0.00 sec)
mysql> select * from employee;
+-------+-----------+--------+------+
| e_id | e_name | salary | d_id |
+-------+-----------+--------+------+
| 12001 | Liu Jing | 6200 | 12 |
| 12002 | Zhang Hong | 5100 | 12 |
| 12003 | Chen Gang | 6800 | 12 |
| 12004 | Li Zhonghua | 4200 | 12 |
| 13001 | Zhou Tao | 7400 | 13 |
| 13002 | Zhang Qiang | 6800 | 13 |
| 13003 | Zhou long | 6250 | 13 |
| 13004 | Zhang Zhongwei | 4520 | 13 |
+-------+-----------+--------+------+
8 rows in set (0.00 sec)
hold 【 Production department 】 The salary of employees increased 50%, The order is as follows :
/* update employee set salary = salary * 1.5 where d_id = ( select d_id from department where d_name = ' Production department ' ); */
mysql> update employee set salary = salary * 1.5
-> where d_id = (
-> select d_id from department where d_name = ' Production department '
-> );
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from employee;
+-------+-----------+--------+------+
| e_id | e_name | salary | d_id |
+-------+-----------+--------+------+
| 12001 | Liu Jing | 6200 | 12 |
| 12002 | Zhang Hong | 5100 | 12 |
| 12003 | Chen Gang | 6800 | 12 |
| 12004 | Li Zhonghua | 4200 | 12 |
| 13001 | Zhou Tao | 11100 | 13 |
| 13002 | Zhang Qiang | 10200 | 13 |
| 13003 | Zhou long | 9375 | 13 |
| 13004 | Zhang Zhongwei | 6780 | 13 |
+-------+-----------+--------+------+
8 rows in set (0.00 sec)
Four 、 Use UPDATE … JOIN Realize Association update
Use UPDATE … JOIN The command can update the current table with data from another table .
The syntax is as follows :
update surface 1 join surface 2 on surface 1. Column 1 = surface 2. Column 2
set surface 1. Column a = surface 2. Column b
for example : There are two tables as follows
mysql> select * from department;
+------+-----------+
| d_id | d_name |
+------+-----------+
| 11 | The sales department |
| 12 | Logistics Department |
| 13 | Production department |
| 14 | R & D department |
+------+-----------+
4 rows in set (0.00 sec)
mysql> select * from employee;
+-------+-----------+--------+------+--------+
| e_id | e_name | salary | d_id | d_name |
+-------+-----------+--------+------+--------+
| 12001 | Liu Jing | 6200 | 12 | NULL |
| 12002 | Zhang Hong | 5100 | 12 | NULL |
| 12003 | Chen Gang | 6800 | 12 | NULL |
| 12004 | Li Zhonghua | 4200 | 12 | NULL |
| 13001 | Zhou Tao | 11100 | 13 | NULL |
| 13002 | Zhang Qiang | 10200 | 13 | NULL |
| 13003 | Zhou long | 9375 | 13 | NULL |
| 13004 | Zhang Zhongwei | 6780 | 13 | NULL |
+-------+-----------+--------+------+--------+
8 rows in set (0.00 sec)
according to department In the table d_name Column data update employee In the table d_name Column :
/* update employee e join department d on e.d_id = d.d_id set e.d_name = d.d_name; */
mysql> update employee e join department d on e.d_id = d.d_id
-> set e.d_name = d.d_name;
Query OK, 8 rows affected (0.01 sec)
Rows matched: 8 Changed: 8 Warnings: 0
mysql> select * from employee;
+-------+-----------+--------+------+-----------+
| e_id | e_name | salary | d_id | d_name |
+-------+-----------+--------+------+-----------+
| 12001 | Liu Jing | 6200 | 12 | Logistics Department |
| 12002 | Zhang Hong | 5100 | 12 | Logistics Department |
| 12003 | Chen Gang | 6800 | 12 | Logistics Department |
| 12004 | Li Zhonghua | 4200 | 12 | Logistics Department |
| 13001 | Zhou Tao | 11100 | 13 | Production department |
| 13002 | Zhang Qiang | 10200 | 13 | Production department |
| 13003 | Zhou long | 9375 | 13 | Production department |
| 13004 | Zhang Zhongwei | 6780 | 13 | Production department |
+-------+-----------+--------+------+-----------+
8 rows in set (0.00 sec)
边栏推荐
- Method of C language annotation
- PayPal subscription process and API request
- Bean Validation使用篇----05
- 这是一道大水题
- [low code] limitations of low code development
- Summary of the fourth week of summer vacation
- AVX instruction set accelerated matrix multiplication
- Xilinx FPGA one way clock input two PLLs
- Bert article translation
- Summary of polynomial commitment schemes
猜你喜欢

MariaDB database upgrade version

【LeetCode第 83 场双周赛】

High number_ Chapter 1 space analytic geometry and vector algebra__ Two point distance

Detailed explanation of data warehouse standard -2022

Programmeur de cheval noir - test d'interface - test d'interface d'apprentissage de quatre jours - jour 4 - Postman lit des fichiers de données externes, lit des données de fichiers de données, IHRM P

Robot dog back submachine gun shooting video fire, netizens shivering: stoooooooopppp!

Bert article translation

Docker pulls the redis image and runs it

XXL job realizes the code parsing of email sending warnings (line by line code interpretation)

数仓数据指标和标签体系区别
随机推荐
Redis cluster hash sharding algorithm (slot location algorithm)
Detailed overview of data standards -2022
Notes and Thoughts on the red dust of the sky (VI) due to lack
【LeetCode第 83 场双周赛】
Notes and Reflections on the red dust of the sky (V) strong culture and weak culture
Dataframe.groupby learning materials
Prometheus+node exporter+grafana monitoring server system resources
Database connection pool & dbutils
An article teaches you the basic use of kubernetes
How can dbcontext support the migration of different databases in efcore advanced SaaS system
Tutorial on principles and applications of database system (045) -- MySQL query (VII): aggregate function
Problem note - unable to open include file: "direct.h": no such file or directory
Bean validation custom container validation chapter ----06
如何使用 SAP Intelligent Robotic Process Automation 自动操作 Excel
Selection method of geometric objects in Creo 9.0
Docker pulls the redis image and runs it
First knowledge of C language functions
Method of C language annotation
Flutter | firstwhere error reporting
How to use mitmproxy to get data return in automated testing?