MySQL-multi-table query (below)

♥️Author : Xiao Liu at Station C

♥️Personal homepage: Xiao Liu's homepage

♥️ Share cloud computing network operation and maintenance classroom notes every day, hard work may not be rewarded, but there will be gains, come on! Work hard together for a better life!

♥️The tree is thousands of feet tall, and the fallen leaves return to the roots. Life is not easy, but the true love in the world

foreword

When we explained the SQL statement before, we explained the DQL statement, that is, the data query statement, but the queries explained before are all single-table queries, and what we want to learn in this chapter is the multi-table query operation. The previous chapter mentioned 5.2 this chapter continue

Table of contents

MySQL

6 subqueries

6.1 Overview

1). Concept

2). Classification

6.2 Scalar Subqueries

case:

6.3 Column query

 case:

6.4 Row Subqueries

6.5 Table subqueries

5.7 Multi-table query case

1). Query employee's name, age, position, department information (implicit inner connection)

2). Query the name, age, position, and department information of employees younger than 30 years old (explicit inner connection)

3). Query the department ID and department name of the employee

4). Query all employees who are older than 40 years old, and the name of the department they belong to; if the employee is not assigned a department, it also needs to be displayed

come (outer join)

5). Query the salary level of all employees

6). Query the information and salary level of all employees in the "R&D Department"

7). Query the average salary of employees in the "R&D department"

8). Query the employee information whose salary is higher than "Extinction".

9). Query employee information with higher than average salary

10). Query the information of employees whose salary is lower than the average salary of the department

11). Query all department information, and count the number of employees in the department

12). Query the course selection of all students, display the student name, student number, course name


MySQL

MySQL is a relational database management system, developed by the Swedish company MySQL AB, which is a product of Oracle. MySQL is one of the most popular relational database management systems. In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System, relational database management system) application software.

    MySQL is a relational database management system. Relational databases store data in different tables instead of putting all the data in one big warehouse, which increases speed and improves flexibility.

    The SQL language used by MySQL is the most commonly used standardized language for accessing databases. MySQL software adopts a dual authorization policy, which is divided into community edition and commercial edition. Due to its small size, fast speed, low overall cost of ownership, especially the open source feature, MySQL is generally chosen as the website for the development of small, medium and large websites. database.

6 subqueries

6.1 Overview

1). Concept

Nested SELECT statements in SQL statements are called nested queries, also known as subqueries.
SELECT * FROM t1 WHERE column1 = ( SELECT column1 FROM t2 );

2). Classification

According to different subquery results, it is divided into:
A. Scalar subquery (subquery result is a single value)
B. Column subquery ( the result of the subquery is a column )
C. Row subquery ( the result of the subquery is one row )
D. Table subquery ( the result of the subquery is multiple rows and multiple columns )
According to the position of the subquery, it is divided into:
A. After WHERE
B. After FROM
C. After SELECT

6.2 Scalar Subqueries

A subquery returns a single value (number, string, date, etc.), and in its simplest form, such a subquery is called a scalar subquery.
Commonly used operators: = <> > >= < <=

case :

A. Query all employee information of " Sales Department "
When completing this requirement, we can decompose the requirement into two steps:
.Query the department ID of " Sales Department "
select id from dept where name = '销售部'; 
. Query employee information according to the department ID of " Sales Department "
select * from emp where dept_id = (select id from dept where name = '销售部');
B. Query employee information after " Fang Dongbai " joins the job
When completing this requirement, we can decompose the requirement into two steps:
.Query Fang Dongbai's entry date
select entrydate from emp where name = '方东白';
.Query the information of employees who joined after the specified entry date
select * from emp where entrydate > (select entrydate from emp where name = '方东
白');

6.3 Column subquery

The result returned by a subquery is a column (can be multiple rows), this subquery is called a column subquery.
Commonly used operators: IN , NOT IN , ANY , SOME , ALL

 case :

A. Query all employee information of " Sales Department " and " Marketing Department "
Break down into the following two steps :
.Query the department IDs of         " Sales Department " and " Marketing Department "
select id from dept where name = '销售部' or name = '市场部'; 
. Query employee information according to the department ID
select * from emp where dept_id in (select id from dept where name = '销售部' or
name = '市场部'); 
B. Query information about employees whose salary is higher than that of everyone in the finance department
broken down into the following two steps
. Query the salary of the owner of the R&D department
select salary from emp where dept_id = (select id from dept where name = '研发部'); 
. Information about employees whose salary is higher than that of any one in the R&D department
select * from emp where salary > any ( select salary from emp where dept_id =
(select id from dept where name = '研发部') );

6.4 Row Subqueries

The result returned by a subquery is a row (can be multiple columns), this subquery is called a row subquery.
Commonly used operators: = , <> , IN , NOT IN

case:

A. Query the same employee information as " Zhang Wuji " 's salary and direct leadership ;
This requirement can also be broken down into two steps :
. Query the salary and direct leadership of " Zhang Wuji "
select salary, managerid from emp where name = '张无忌'; 
. Query the same employee information as " Zhang Wuji " 's salary and direct leadership ;
select * from emp where (salary,managerid) = (select salary, managerid from emp
where name = '张无忌');

6.5 Table subqueries

The result returned by a subquery is multiple rows and multiple columns. This kind of subquery is called a table subquery.
Commonly used operators: IN
Case: A. Query the employee information with the same position and salary as " Luzhangke " and " Song Yuanqiao "
Decomposed into two steps: ① Query the positions and salaries of " Luzhangke " and " Song Yuanqiao "
select job, salary from emp where name = '鹿杖客' or name = '宋远桥'; 1
.Query information about employees with the same position and salary as " Luzhangke " and " Song Yuanqiao "
select * from emp where (job,salary) in ( select job, salary from emp where name =
'鹿杖客' or name = '宋远桥' );
B. Query the employee information and department information whose entry date is after "2006-01-01"
Decomposed into two steps :
.Employee information whose entry date is after "2006-01-01"
select * from emp where entrydate > '2006-01-01';
. Query this part of employees and the corresponding department information ;
select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left
join dept d on e.dept_id = d.id ;

5.7 Multi-table query case

Data environment preparation :
create table salgrade(
grade int,
losal int,
hisal int
) comment '薪资等级表';
insert into salgrade values (1,0,3000);
insert into salgrade values (2,3001,5000);
insert into salgrade values (3,5001,8000);
insert into salgrade values (4,8001,10000);
insert into salgrade values (5,10001,15000);
insert into salgrade values (6,15001,20000);
insert into salgrade values (7,20001,25000);
insert into salgrade values (8,25001,30000);
In this case, we mainly use the multi-table query syntax explained above to complete the following 12 requirements, and here mainly involves
There are only three tables: emp employee table, dept department table, and salgrade salary scale table.

1). Query employee's name, age, position, department information (implicit inner connection)

Table : emp , dept
Join condition : emp.dept_id = dept.id
select e.name , e.age , e.job , d.name from emp e , dept d where e.dept_id = d.id;

2). Query the name, age, position, and department information of employees younger than 30 years old (explicit inner connection)

Table : emp , dept
Join condition : emp.dept_id = dept.id
select e.name , e.age , e.job , d.name from emp e inner join dept d on e.dept_id =
d.id where e.age < 30;

3). Query the department ID and department name of the employee

Table : emp , dept
Join condition : emp.dept_id = dept.id
select distinct d.id , d.name from emp e , dept d where e.dept_id = d.id; 

4). Query all employees who are older than 40 years old , and the name of the department they belong to ; if the employee is not assigned a department , it also needs to be displayed

come ( outer join )

Table : emp , dept
Join condition : emp.dept_id = dept.id
select e.*, d.name from emp e left join dept d on e.dept_id = d.id where e.age >
40 ; 

5). Query the salary level of all employees

  
Table : emp, salgrade
Join conditions : emp.salary >= salgrade.losal and emp.salary <= salgrade.hisal
-- 方式一
select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary >=
s.losal and e.salary <= s.hisal;
-- 方式二
select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary
between s.losal and s.hisal;

6). Query the information and salary levels of all employees in the " R&D Department "

Table : emp , salgrade , dept
Join condition : emp.salary between salgrade.losal and salgrade.hisal ,
emp.dept_id = dept.id
Query conditions : dept.name = ' R&D Department '
select e.* , s.grade from emp e , dept d , salgrade s where e.dept_id = d.id and (
e.salary between s.losal and s.hisal ) and d.name = '研发部';

7). Query the average salary of employees in the " R&D Department "

Table : emp , dept
Join condition : emp.dept_id = dept.id
select avg(e.salary) from emp e, dept d where e.dept_id = d.id and d.name = '研发
部';

8). Query information about employees whose salary is higher than " Extinction " .

.Query the salary of " Extinction "
   
select salary from emp where name = '灭绝';
.Query employee data with higher salary than her
select * from emp where salary > ( select salary from emp where name = '灭绝' );

9). Query information about employees whose salary is higher than the average

.Query the average salary of employees
select avg(salary) from emp;
. Query the information of employees whose salary is higher than the average
select * from emp where salary > ( select avg(salary) from emp ); 1

10). Query the information of employees whose salary is lower than the average salary of the department

.Query the average salary of the specified department
select avg(e1.salary) from emp e1 where e1.dept_id = 1;
select avg(e1.salary) from emp e1 where e1.dept_id = 2;
. Query the information of employees whose salary is lower than the average salary of the department
select * from emp e2 where e2.salary < ( select avg(e1.salary) from emp e1 where
e1.dept_id = e2.dept_id );

11). Query all department information and count the number of employees in the department

select d.id, d.name , ( select count(*) from emp e where e.dept_id = d.id ) '人数'
from dept d;

12). Query the course selection of all students , display the student name , student number , and course name

: student , course , student_course
Join condition : student.id = student_course.studentid , course.id =
student_course.courseid
select s.name , s.no , c.name from student s , student_course sc , course c where
s.id = sc.studentid and sc.courseid = c.id ;
Remarks : There may be many ways to realize the above requirements , and there are also many SQL writing methods. As long as our needs can be met, we can query the records that meet the conditions.

♥️Following is the driving force for my creation

♥️Like, is the greatest recognition for me

♥️This is Xiaoliu, I am inspiring to do every article well, thank you everyone

Guess you like

Origin blog.csdn.net/lzl10211345/article/details/130832535