SQL authoring for seven joins

Source: Internet
Author: User
Tags joins

First, join diagram

Ii. SQL Demo

A. Creating demo tables and data

SET NAMES utf8mb4; SET foreign_key_checks = 0;--------------------------------Table structure for ' tbl_dept '--------------------------- ---DROP TABLE IF EXISTS ' tbl_dept '; CREATE TABLE ' tbl_dept ' (' id ' int (one) not null auto_increment, ' deptname ' varchar () DEFAULT NULL, ' locadd ' varchar (4 0) Default NULL, PRIMARY KEY (' id ')) engine=innodb auto_increment=7 default Charset=utf8;------------------------------  --Records of ' tbl_dept '------------------------------begin;insert into ' tbl_dept ' VALUES (' 1 ', ' RD ', ' One '), (' 2 ', ' HR '), ' 3 ', ' MK ', ' + '), (' 5 ', ' MIS ', ' + '), (' 6 ', ' FD ', ' 15 '); COMMIT;--------------------------------table structure for ' tbl_emp '------------------------------DROP table IF EXISTS ' tbl_emp '; CREATE TABLE ' tbl_emp ' (' id ' int (one) not null auto_increment, ' name ' varchar) DEFAULT NULL, ' deptid ' int (one) defaul T NULL, PRIMARY key (' ID '), key ' fk_dept_id ' (' DeptID ')) Engine=innodb auto_increment=9 DEFAULT Charset=utf8;---------- ----------------------Records of ' tbl_emp '------------------------------begin;insert into ' tbl_emp ' VALUES (' 1 ', ' Z3 ', ' 1 '), (' 2 ', ' Z4 ', ' 1 '), (' 3 ', ' Z5 ', ' 1 '), (' 4 ', ' W5 ', ' 2 '), (' 5 ', ' W6 ', ' 2 '), (' 6 ', ' S7 ', ' 3 '), (' 7 ', ' S8 ', ' 4 '), (' 8 ', ' S9 ', ' 51 '); COMMIT; SET foreign_key_checks = 1;

B. Specific SQL and results

1. Figure I

SELECT * from Tbl_emp a left JOIN tbl_dept b on a.deptid = b.ID;

2. Figure II

SELECT * from Tbl_emp a INNER joins tbl_dept b on a.deptid = b.ID;

3. Figure III

SELECT * from Tbl_emp a right joins tbl_dept b on a.deptid = b.ID;

4. Figure Four

SELECT * from Tbl_emp a left JOIN tbl_dept b to A.deptid = b.id where b.id is null;

5. Figure Five

SELECT * from Tbl_emp a right JOIN tbl_dept b to A.deptid = b.id where a.deptid is null;

6. Figure Six

SELECT * from Tbl_emp a right joins tbl_dept b on a.deptid = B.idunionselect * from Tbl_emp a right joins Tbl_dept B on a.de PtId = b.id;

7. Figure Seven

SELECT * FROM Tbl_emp a left joins tbl_dept b on a.deptid = b.id where b.id are nullunionselect * from Tbl_emp a RIGHT Join Tbl_dept b on a.deptid = b.id where a.deptid is null;

This concludes ...

SQL authoring for seven joins

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.