Internal connection, left join, right connection in MySQL database

Source: Internet
Author: User

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

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.