25. Cross connection in MySQL

1 Introduction

The result returned by the cross join is the Cartesian product of all data rows in the two connected tables . It should be noted that the result of the cross connection is a Cartesian product, and it has no practical application .

For example, if there are 3 fields and 4 records in the class table, and 5 fields and 10 records in the student table, then the Cartesian product after cross-connection is equal to 4 * 10 records, each record contains 3+ 5 fields.

grammar

1  SELECT query field FROM table 1 CROSS  JOIN table 2;

CROSS JOIN is used to join two tables to be queried.

2. Preparation

 1 CREATE DATABASE mahaiwuji;
 2 USE mahaiwuji;
 3 
 4 CREATE TABLE grade(
 5     gid INT(4) PRIMARY KEY,
 6     gname VARCHAR(36),
 7     tehcher VARCHAR(36)
 8 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
 9 
10 INSERT INTO grade VALUES (1 , ' One Class ' , ' One Class Teacher ' );
 11  INSERT  INTO grade VALUES ( 2 , ' Second Class ' , ' Second Class Teacher ' );
 12  INSERT  INTO grade VALUES ( 3 , ' Three Class ' , ' Three Class Teacher ' );
 13  INSERT  INTO grade VALUES ( 4 , ' Four Classes ' , '四班老师');
14 
15 CREATE TABLE student(
16     sid INT(4) PRIMARY KEY,
17     sname VARCHAR(36),
18     sex VARCHAR(10),
19     score FLOAT,
20     height FLOAT    
21 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
22 
23 INSERT INTO student VALUES (1,'a1','',95.6,172.5);
24 INSERT INTO student VALUES (2,'a2','',84.6,172.5);
25 INSERT INTO student VALUES (3,'a3','',94.6,172.5);
26 INSERT INTO student VALUES (4,'a4','',95.6,172.5);
27 INSERT INTO student VALUES (5,'a5','',55.6,172.5);
28 INSERT INTO student VALUES (6,'a6','',25.6,172.5);
29 INSERT INTO student VALUES (7,'a7','',35.6,172.5);
30 INSERT INTO student VALUES (8,'a8','',89.6,172.5);
31 INSERT INTO student VALUES (9,'a9','',95.6,172.5);
32 INSERT INTO student VALUES (10,'a10','',95.6,172.5);

3. Case

1 SELECT * FROM grade CROSS JOIN student;

 

Guess you like

Origin www.cnblogs.com/mahaiwuji/p/12710321.html