Original: http://www.cnblogs.com/xwdreamer/archive/2010/12/15/2297058.html
Internal connection: The data corresponding to two tables are isolated
Outer joins: Isolate the corresponding data based on a table
First create the table in the database with the following database code:
/*Navicat MySQL Data transfersource server:localhost_3306source Server version:50150source Host: Localhost:3306source database:storetarget Server type:mysqltarget server Version:50150file Encoding : 65001date:2010-12-15 16:27:53*/SETForeign_key_checks=0;-- ------------------------------Table structure for ' grade '-- ----------------------------DROP TABLE IF EXISTS' grade ';CREATE TABLE' Grade ' (' No ' )int( One) not NULLauto_increment, ' Grade 'int( One) not NULL, PRIMARY KEY(' no ')) ENGINE=InnoDB auto_increment=4 DEFAULTCHARSET=UTF8;-- ------------------------------Records of Grade-- ----------------------------INSERT intoGradeVALUES('1',' -');INSERT intoGradeVALUES('2',' the');INSERT intoGradeVALUES('3',' -');-- ------------------------------Table structure for ' student '-- ----------------------------DROP TABLE IF EXISTS' student ';CREATE TABLE' student ' (' No ' )int( One) not NULLauto_increment, ' name 'varchar( -) not NULL, PRIMARY KEY(' no ')) ENGINE=InnoDB auto_increment=5 DEFAULTCHARSET=UTF8;-- ------------------------------Records of Student-- ----------------------------INSERT intoStudentVALUES('1','a');INSERT intoStudentVALUES('2','b');INSERT intoStudentVALUES('3','C');INSERT intoStudentVALUES('4','D');
The fields in the student table are no and the fields in the Name,grade table are no and grade, respectively. No in both tables represents the student's school number.
Query the results of the student table:
Mysql> Select * fromgrade;+----+-------+|No|Grade|+----+-------+| 1 | - || 2 | the || 3 | - |+----+-------+Rowsinch Set
Query the results of the grade table:
Mysql> Select * fromStudent SInner JoinGrade G onS.No=g.no;+----+------+----+-------+|No|Name|No|Grade|+----+------+----+-------+| 1 |A| 1 | - || 2 |B| 2 | the || 3 |C| 3 | - |+----+------+----+-------+Rowsinch Set
left connection (all data in the left table, corresponding data in the right table)
Mysql> Select * fromStudent asS Left JoinGrade asg onS.No=g.no;+----+------+------+-------+|No|Name|No|Grade|+----+------+------+-------+| 1 |A| 1 | - || 2 |B| 2 | the || 3 |C| 3 | - || 4 |D| NULL | NULL |+----+------+------+-------+Rowsinch Set
Right connection (all data in the right table, corresponding data in the left table)
Mysql> Select * fromStudent asS Right JoinGrade asG onS.No=g.no;+----+------+----+-------+|No|Name|No|Grade|+----+------+----+-------+| 1 |A| 1 | - || 2 |B| 2 | the || 3 |C| 3 | - |+----+------+----+-------+Rowsinch Set
Internal connection, left join, right connection in MySQL database