25.MySQL中的交叉连接CROSS JOIN

1.简介

交叉连接返回的结果是被连接的两个表中所有数据行的笛卡尔积。需要注意的是,交叉连接产生的结果是笛卡尔积,并没有实际应用的意义

例如,班级表中有3个字段,4条记录,学生表中有5个字段,10条记录,那么交叉连接后的笛卡尔积就等于4*10条记录数,每条记录中含有3+5个字段。

语法

1 SELECT 查询字段 FROM 表1 CROSS JOIN 表2;

CROSS JOIN用于连接两个要查询的表。

2.准备

 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,一班,一班老师);
11 INSERT INTO grade VALUES (2,二班,二班老师);
12 INSERT INTO grade VALUES (3,三班,三班老师);
13 INSERT INTO grade VALUES (4,四班,四班老师);
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.案例

1 SELECT * FROM grade CROSS JOIN student;

 

25.MySQL中的交叉连接CROSS JOIN

上一篇:mysql 日期字段占用空间大小


下一篇:MySQL索引的数据结构-B+树介绍