MySQL multi-table connection query

Source: Internet
Author: User
Tags joins

MySQL multi-table connection query

Multi-table Connection query:

CREATE TABLE Class (

CID int primary Key auto_increment,

CNAME varchar (20)

) default charset= ' UTF8 ';

Create Tablr Stu (

Sid Int Primary Key auto_increment,

sname varchar (20).

CID int

);d efault charset= ' UTF8 ';

INSERT into Class (name) values (' one shift ');

INSERT into Class (name) values (' Class two ');

INSERT into Class (name) values (' Class three ');

INSERT into Class (name) values (' Class four ');

INSERT into Class (name) VALUES (' Zhang San ', 1);

INSERT into Class (name) VALUES (' John Doe ', 2);

INSERT into Class (name) VALUES (' Harry ', 2);

INSERT into class values (' Yang Six ', 3);

INSERT into Class (name) values (' Zhao Qi ', 5);

INSERT into Class (name) values (' Week eight ', 6);

SELECT * from class;

SELECT * from Stu;

-------------------------------------

Cross join:--no conditions, just to show all the data in multiple tables

SELECT * from Class,stu;

Internal connection: Established between two or more tables, there is a correlation between the data

INNER JOIN

SELECT * FROM Table a INNER join table B on join condition;

Equivalent to

SELECT * FROM Table A, table B where table A. field = table B. field;

Two tables:

SELECT * FROM class INNER joins Stu on Class.cid=stu.cid;

SELECT * from Class,stu, table C WHERE class.cid=stu.cid and table A. field = Table C. Field;

Three sheets:

SELECT * FROM student s,course C,score SC WHERE s.sid=sc.sid and c.cid=sc.cid;

External connection:

Left outer connection Join/left OUTER join

Right outer connection Join/right OUTER join

Outer joins: In the case of the left or right table as the primary table, the entire contents of the main table are displayed, and the other table does not have the corresponding data to fill the null

SELECT * FROM class C left joins Stu S on C.cid=s.cid;

SELECT * FROM class C Right joins Stu S on C.cid=s.cid;

MySQL multi-table connection query

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.