Build table

In this case, let’s set up two tables that have foreign keys associated with them.

CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` INT(11) NOT NULL AUTO_INCREMENT, `deptName` VARCHAR(30), `locAdd` VARCHAR(40), PRIMARY KEY (`id`) ) ENGINE=INNODB CHARSET=utf8; CREATE TABLE `db0206`.`tbl_emp`( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(20), `deptId` INT(11), PRIMARY KEY (`id`), FOREIGN KEY (`deptId`) REFERENCES `db0206`.`tb_dept`(`id`) ) ENGINE=INNODB CHARSET=utf8; /* 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,deptId) VALUES('z3',1); INSERT INTO tbl_emp(NAME,deptId) VALUES('z4',1); INSERT INTO tbl_emp(NAME,deptId) VALUES('z5',1); INSERT INTO tbl_emp(NAME,deptId) VALUES('w5',2); INSERT INTO tbl_emp(NAME,deptId) VALUES('w6',2); INSERT INTO tbl_emp(NAME,deptId) VALUES('s7',3); INSERT INTO tbl_emp(NAME,deptId) VALUES('s8',4);Copy the code

Venn diagram and SQL statement preparation and query results

In the connection

Internally connected Venn diagram

The SQL statement executed and the query result executed

  • SQL statement executed
select * from tbl_dept a inner join tbl_emp b on a.id=b.deptId;
Copy the code
  • The query results

The left outer join

Connect the Venn diagram to the left

The SQL statement executed and the query result executed

  • SQL statement executed
select * from tbl_dept a left join tbl_emp b on a.id=b.deptId;
Copy the code
  • The query results

Right connection

Connect the Venn diagram on the outside right

The SQL statement executed and the query result executed

  • SQL statement executed
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId;
Copy the code
  • The query results

Left connection

Left link Venn diagram

The SQL statement executed and the query result executed

  • SQL statement executed
elect * from tbl_dept a left join tbl_emp b on a.id=b.deptId where b.deptId is null;
Copy the code
  • The query results

The right connection

Connect the Venn diagram on the right

The SQL statement executed and the query result executed

  • SQL statement executed
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId where a.id is null;
Copy the code
  • The query results

All connection

Fully connected Venn diagram

The SQL statement executed and the query result executed

  • SQL statement executed
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId 
union 
select * from tbl_dept a left join tbl_emp b on a.id=b.deptId;
Copy the code
  • The query results

Data sets that do not appear in either table

Venn diagram

The SQL statement executed and the query result executed

  • SQL statement executed
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId where a.id is null union select * from tbl_dept a left join tbl_emp b on a.id=b.deptId where b.deptId is null;
Copy the code
  • The query results

From: www.cnblogs.com/dinglinyong…