(1) Prepare the environment
1) Create employee table
mysql> create table company.employee6(
-> emp_id int auto_increment primary key not null,
-> emp_name varchar(10),
-> age int,
-> dept_id int);
mysql> insert into employee6(emp_name,age,dept_id) values
-> ('tom',19,200),
-> ('jack',30,201),
-> ('alice',24,202),
-> ('robin',40,200),
-> ('natasha',28,204);
2) Create a department table
mysql> create table company.department(
-> dept_id int,
-> dept_name varchar(100));
mysql> insert into department values (200,'hr'), (201,'it'), (202,'sale'), (203,'fd');
(2) Cross-connect
Generate a Cartesian product without using any matching conditions
Syntax: select table1.field1,table1.field2,table2.field1 from table1,table2;
mysql> select employee6.emp_name,employee6.age,employee6.dept_id,department.dept_name from employee6,department;
+----------+------+---------+-----------+
| emp_name | age | dept_id | dept_name |
+----------+------+---------+-----------+
| tom | 19 | 200 | hr |
| tom | 19 | 200 | it |
| tom | 19 | 200 | sale |
| tom | 19 | 200 | fd |
| jack | 30 | 201 | hr |
| jack | 30 | 201 | it |
| jack | 30 | 201 | sale |
| jack | 30 | 201 | fd |
| alice | 24 | 202 | hr |
| alice | 24 | 202 | it |
| alice | 24 | 202 | sale |
| alice | 24 | 202 | fd |
| robin | 40 | 200 | hr |
| robin | 40 | 200 | it |
| robin | 40 | 200 | sale |
| robin | 40 | 200 | fd |
| natasha | 28 | 204 | hr |
| natasha | 28 | 204 | it |
| natasha | 28 | 204 | sale |
| natasha | 28 | 204 | fd |
+----------+------+---------+-----------+
(3) Inner join: connect only matching rows based on the same fields of the two tables
Syntax: select table 1. field n, table 2. field n from table 1, table 2 table 1. field = table 2. field
Connect according to the dept_id of the employee table and the dept_id of the department table, and only match the rows with the same dept_id
mysql> select employee6.dept_id,employee6.emp_name,employee6.age,department.dept_name from employee6,department where employee6.dept_id = department.dept_id;
+---------+----------+------+-----------+
| dept_id | emp_name | age | dept_name |
+---------+----------+------+-----------+
| 200 | tom | 19 | hr |
| 201 | jack | 30 | it |
| 202 | alice | 24 | sale |
| 200 | robin | 40 | hr |
+---------+----------+------+-----------+
(4) Outer join
Syntax: select field list from table1 left|right join table2 on table1.field = table2.field
1) Left join of outer join: All values in the left table will be displayed, regardless of whether they match or not in the right table
mysql> select emp_id,emp_name,age,dept_name from employee6 left join department on employee6.dept_id = department.dept_id;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tom | 19 | hr |
| 4 | robin | 40 | hr |
| 2 | jack | 30 | it |
| 3 | alice | 24 | sale |
| 5 | natasha | 28 | NULL |
+--------+----------+------+-----------+
2) Right join of outer join: All values in the right table will be displayed, regardless of whether they match or not in the left table
mysql> select emp_id,emp_name,age,dept_name from employee6 right join department on employee6.dept_id = department.dept_id;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tom | 19 | hr |
| 2 | jack | 30 | it |
| 3 | alice | 24 | sale |
| 4 | robin | 40 | hr |
| NULL | NULL | NULL | fd |
+--------+----------+------+-----------+
(5) Compound conditional connection query
Query employee6 and department tables in an inner join, and the age field value in employee6 table must be greater than 25, sort
mysql> select emp_id,emp_name,age,dept_name from employee6,department where employee6.dept_id = department.dept_id and age >25;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 4 | robin | 40 | hr |
| 2 | jack | 30 | it |
+--------+----------+------+-----------+
2 rows in set (0.00 sec)
mysql> select emp_id,emp_name,age,dept_name from employee6,department where employee6.dept_id = department.dept_id and age >25 order by age;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 2 | jack | 30 | it |
| 4 | robin | 40 | hr |
+--------+----------+------+-----------+
2 rows in set (0.00 sec)
mysql> select emp_id,emp_name,age,dept_name from employee6,department where employee6.dept_id = department.dept_id and age >25 order by age desc;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 4 | robin | 40 | hr |
| 2 | jack | 30 | it |
+--------+----------+------+-----------+
(6) Subquery
A subquery is a query statement nested within another query statement. The query result of the inner query statement can provide query conditions for the outer query statement.
1) Subquery with in
mysql> select * from employee6 where dept_id in (select dept_id from department);
+--------+----------+------+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+------+---------+
| 1 | tom | 19 | 200 |
| 2 | jack | 30 | 201 |
| 3 | alice | 24 | 202 |
| 4 | robin | 40 | 200 |
+--------+----------+------+---------+
4 rows in set (0.00 sec)
2) Subqueries with comparison operators
mysql> select dept_name from department where dept_id in (select dept_id from employee6 where age >25);
+-----------+
| dept_name |
+-----------+
| it |
| hr |
+-----------+
2 rows in set (0.00 sec)