mysql execution order is connected to join

mysql load order

Handwritten order

SELECT DISTINCT
    <select list>
FROM 
    <left_table> join <join_type> JOIN <right_table> ON <join_condition>
WHERE
    <where_condition>
GROUP BY
    <group_by_list>     
HAVING
    <having_condition>  
ORDER BY
    <order_by_condition>
LIMIT <limit_number>    

Machine Readable order

 1. FROM <left_table>
 2. ON <join_condition>
 3. <join_type> JOIN <right_table>
 4. WHERE <where_condition>
 5. GROUP BY <group_by_list> 
 6. HAVING <having_condition>
 7. SELECT 
 8. DISTINCT <select list>
 9. ORDER BY <order_by_condition>
 10. LIMIT <limit_number>

Sql statement execution order can be used to represent this fishbone

Even join table

mysqlEven in the basic table can be divided into the following.

Next these different kinds write the corresponding sqlstatement.

The first is to create the appropriate table to practice.

create table if not exists tbl_dept(
    id int not null auto_increment primary key,
    deptName varchar(30),
    locAdd varchar(40)
);

create table if not exists tbl_emp(
        id int auto_increment primary key,
        name varchar(20),
        depid int
);

insert into tbl_dept(deptName, locAdd) values('RD', 11);
insert into tbl_dept(deptName, locAdd) values('HR', 12);
insert into tbl_dept(deptName, locAdd) values('MK', 13);
insert into tbl_dept(deptName, locAdd) values('MIS', 14);
insert into tbl_dept(deptName, locAdd) values('FD', 15);

insert into tbl_emp(name, depid) values('z3', 1);
insert into tbl_emp(name, depid) values('z4', 1);
insert into tbl_emp(name, depid) values('z5', 1);
insert into tbl_emp(name, depid) values('w5', 2);
insert into tbl_emp(name, depid) values('w6', 2);
insert into tbl_emp(name, depid) values('s7', 3);
insert into tbl_emp(name, depid) values('s8', 4);
insert into tbl_emp(name, depid) values('s9', 51);

The connector (equivalent connections)

mysql> select * from tbl_emp as e inner join tbl_dept as d on e.depid=d.id;
+----+------+-------+----+----------+--------+
| id | name | depid | id | deptName | locAdd |
+----+------+-------+----+----------+--------+
|  1 | z3   |     1 |  1 | RD       | 11     |
|  2 | z4   |     1 |  1 | RD       | 11     |
|  3 | z5   |     1 |  1 | RD       | 11     |
|  4 | w5   |     2 |  2 | HR       | 12     |
|  5 | w6   |     2 |  2 | HR       | 12     |
|  6 | s7   |     3 |  3 | MK       | 13     |
|  7 | s8   |     4 |  4 | MIS      | 14     |
+----+------+-------+----+----------+--------+
7 rows in set (0.01 sec)

Left connector (connecting all left table, right table filled missing fields to null)

mysql> select * from tbl_emp as e left join tbl_dept as d on e.depid=d.id;
+----+------+-------+------+----------+--------+
| id | name | depid | id   | deptName | locAdd |
+----+------+-------+------+----------+--------+
|  1 | z3   |     1 |    1 | RD       | 11     |
|  2 | z4   |     1 |    1 | RD       | 11     |
|  3 | z5   |     1 |    1 | RD       | 11     |
|  4 | w5   |     2 |    2 | HR       | 12     |
|  5 | w6   |     2 |    2 | HR       | 12     |
|  6 | s7   |     3 |    3 | MK       | 13     |
|  7 | s8   |     4 |    4 | MIS      | 14     |
|  8 | s9   |    51 | NULL | NULL     | NULL   |
+----+------+-------+------+----------+--------+
8 rows in set (0.03 sec)

Right connection (connection table of all the right, the left table filled missing fields to null)

mysql> select * from tbl_emp as e right join tbl_dept as d on e.depid=d.id;
+------+------+-------+----+----------+--------+
| id   | name | depid | id | deptName | locAdd |
+------+------+-------+----+----------+--------+
|    1 | z3   |     1 |  1 | RD       | 11     |
|    2 | z4   |     1 |  1 | RD       | 11     |
|    3 | z5   |     1 |  1 | RD       | 11     |
|    4 | w5   |     2 |  2 | HR       | 12     |
|    5 | w6   |     2 |  2 | HR       | 12     |
|    6 | s7   |     3 |  3 | MK       | 13     |
|    7 | s8   |     4 |  4 | MIS      | 14     |
| NULL | NULL | NULL  |  5 | FD       | 15     |
+------+------+-------+----+----------+--------+
8 rows in set (0.03 sec)

Left exclusive connection

mysql> select * from tbl_emp as e left join tbl_dept as d on e.depid=d.id where d.id is null;
+----+------+-------+------+----------+--------+
| id | name | depid | id   | deptName | locAdd |
+----+------+-------+------+----------+--------+
|  8 | s9   |    51 | NULL | NULL     | NULL   |
+----+------+-------+------+----------+--------+
1 row in set (0.04 sec)

Right exclusive connection

mysql> select * from tbl_emp as e right join tbl_dept as d on e.depid=d.id where e.id is null;
+------+------+-------+----+----------+--------+
| id   | name | depid | id | deptName | locAdd |
+------+------+-------+----+----------+--------+
| NULL | NULL | NULL  |  5 | FD       | 15     |
+------+------+-------+----+----------+--------+
1 row in set (0.04 sec)

Fully connected

Because mysqlnot supported fully connected, so you need to use unionfor simulation.

mysql> select * from tbl_emp as e left join tbl_dept as d on e.depid=d.id
union
select * from tbl_emp as e right join tbl_dept as d on e.depid=d.id;
+------+------+-------+------+----------+--------+
| id   | name | depid | id   | deptName | locAdd |
+------+------+-------+------+----------+--------+
|    1 | z3   |     1 |    1 | RD       | 11     |
|    2 | z4   |     1 |    1 | RD       | 11     |
|    3 | z5   |     1 |    1 | RD       | 11     |
|    4 | w5   |     2 |    2 | HR       | 12     |
|    5 | w6   |     2 |    2 | HR       | 12     |
|    6 | s7   |     3 |    3 | MK       | 13     |
|    7 | s8   |     4 |    4 | MIS      | 14     |
|    8 | s9   |    51 | NULL | NULL     | NULL   |
| NULL | NULL | NULL  |    5 | FD       | 15     |
+------+------+-------+------+----------+--------+
9 rows in set (0.04 sec)

Left + Right exclusive connection exclusive connection

Similarly union connection using analog

mysql> select * from tbl_emp as e left join tbl_dept as d on e.depid=d.id where d.id is null
union
select * from tbl_emp as e right join tbl_dept as d on e.depid=d.id where e.id is null;
+------+------+-------+------+----------+--------+
| id   | name | depid | id   | deptName | locAdd |
+------+------+-------+------+----------+--------+
|    8 | s9   |    51 | NULL | NULL     | NULL   |
| NULL | NULL | NULL  |    5 | FD       | 15     |
+------+------+-------+------+----------+--------+
2 rows in set (0.04 sec)

Guess you like

Origin www.cnblogs.com/yscl/p/12043598.html