Seven types of join in MySQL and seven types of join in mysql

Source: Internet
Author: User

Seven types of join in MySQL and seven types of join in mysql
Create a table

Here we will first create two tables with foreign key associations.

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 ('demotid') REFERENCES 'db0206 '. 'tb _ dept' ('id') ENGINE = innodb charset = utf8;/* INSERT data */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 ('ss', 4 );
The writer's diagram is connected to the SQL statement writing and query results.

Executed SQL statements and query results
  • Executed SQL statement
select * from tbl_dept a inner join tbl_emp b on a.id=b.deptId;
  • Query Result
Left Outer Join Wen's diagram

Executed SQL statements and query results
  • Executed SQL statement
select * from tbl_dept a left join tbl_emp b on a.id=b.deptId;
  • Query Result
Right outer join right Outer Join Wen's diagram

Executed SQL statements and query results
  • Executed SQL statement
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId;
  • Query Result
Left join left join Wen's diagram

Executed SQL statements and query results
  • Executed SQL statement
elect * from tbl_dept a left join tbl_emp b on a.id=b.deptId where b.deptId is null;
  • Query Result

Graph of right join and right join

Executed SQL statements and query results
  • Executed SQL statement
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId where a.id is null;
  • Query Result

Graph of Full-connection and full-connection

Executed SQL statements and query results
  • Executed SQL statement
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;
  • Query Result
A dataset's diagram that does not appear in both tables

Executed SQL statements and query results
  • Executed SQL statement
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;
  • Query Result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.