当前位置:网站首页>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)
边栏推荐
- 如何在自动化测试中使用MitmProxy获取数据返回?
- 多源文件方式去访问全局变量的方式(extern用法)
- MySQL table field quantity limit and row size limit
- How to realize 485 wireless communication between multiple sensors and Siemens PLC?
- Tutorial on the principle and application of database system (046) -- MySQL query (VIII): group by
- 项目场景:nvidia-smi Unable to datemine the device handle for GPU 0000:01:00.0: Unknow Error
- AWS Part 4 one machine and one secret
- postman测试接口在URL配置正确的情况下出现404或者500错误
- 【电赛训练】非接触物体尺寸形态测量 2020年电赛G题
- C language book recommendation
猜你喜欢

Installation and use of appscan

Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?

mysql 分支语句case报错

網絡系統實驗:ping不通的問題解决

postman测试接口在URL配置正确的情况下出现404或者500错误

Classic example of C language - loan balance

网络系统实验:ping不通的问题解决

What is the function of the select... For UPDATE statement? Can you lock tables or rows?

Creo 9.0 mouse button operation for model observation

High number_ Chapter 1 space analytic geometry and vector algebra__ Two point distance
随机推荐
How can dbcontext support the migration of different databases in efcore advanced SaaS system
Communication module sorting (II) hc-05
Case error of MySQL branch statement
Bean Validation使用篇----05
Design details related to sap e-commerce cloud Spartacus UI store
黑马程序员-接口测试-四天学习接口测试-第四天-Postman读取外部数据文件,读取数据文件数据,iHRM项目实战,员工管理模块,添加员工,批量运行测试用例,生成测试报告,
C language macro definition
CA digital certificate
AWS Part 4 one machine and one secret
Don't let Fujin Yibo see this
freemarker
Classic example of C language - convert the input two digits into English
MySQL table field quantity limit and row size limit
AVX instruction set accelerated matrix multiplication
【电赛训练】非接触物体尺寸形态测量 2020年电赛G题
The way to access global variables in multi-source file mode (extern usage)
MariaDB database upgrade version
Reverse linked list drawing demonstration
The salary of a tester who has worked for 3 years after job hopping is twice that of the original. The secret is
黑馬程序員-接口測試-四天學習接口測試-第四天-Postman讀取外部數據文件,讀取數據文件數據,iHRM項目實戰,員工管理模塊,添加員工,批量運行測試用例,生成測試報告,