SQL——交叉联接(CROSS JOIN)

版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/88171166

交叉连接的操作,它们都返回被连接的两个表所有数据行的笛卡尔积,返回到的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。惟一的不同在于,交叉连接分开列名时,使用CROSS JOIN关键字而不是逗号。

一个交叉联接(CROSS JOIN)接收两个分别有N行和M行的表T1和T2,然后返回一个包含交叉乘积 N×M 条记录的联接表。

例如,l我们想知道学生和课程有多少可能的组合

----学生表
create table t_student
(
  Sid  char(5) not null  primary key, --学号
  Sname varchar2(10) not null,  --姓名
  Ssex  char(1) not null,   --性别
  Sbirthday  date  null,  --出生日期
  Stel varchar2(13) null, --联系电话
  Sclass number(2)  null  --班级
);
insert into t_student values('10001','谢娜','f','1-4月-1986','13697855678',1);
insert into t_student values('10002','姚明','m','6-6月-1978','13680888086',2);
insert into t_student values('10003','马云','m','23-5月-1986','13520035611',1);
insert into t_student values('10004','张朝阳','m','8-9月-1971','15597844378',3);
insert into t_student values('10005','杨澜','f','3-7月-1973','13897856666',2);
insert into t_student values('10006','白岩松','m','11-11月-1972','13266668888',3);
insert into t_student values('10007','李娜','m','11-11月-1972','13266668888',3);

select * from t_student;

----课程表
create table t_course(
  Cid number(2) not null primary key,      --课程编号
  Cname varchar2(40) not null, --课程名称
  Ctype char(6)  not null,      --课程类型 (必修/选修)
  Chours number(3)  not null   --课时数
);
insert into t_course values(1,'操作系统','选修',32);
insert into t_course values(2,'数据结构','选修',48);
insert into t_course values(3,'教育心理学','选修',24);
insert into t_course values(4,'java核心技术','必修',64);
insert into t_course values(5,'java web开发技术','必修',48);
insert into t_course values(6,'java框架技术','必修',64);


select * from t_course;

----成绩表
--drop table t_score;
create table t_score(
  Sid char(5) not null ,
  Cid number(2) not null ,      --课程编号
  Score number(3) 
);

insert into t_score values('10001',1,56);
insert into t_score values('10001',2,46);
insert into t_score values('10001',3,37);
insert into t_score values('10001',4,59);
insert into t_score values('10001',5,55);
insert into t_score values('10001',6,49);

insert into t_score values('10002',1,76);
insert into t_score values('10002',2,66);
insert into t_score values('10002',3,57);
insert into t_score values('10002',4,79);
insert into t_score values('10002',5,75);
insert into t_score values('10002',6,69);

insert into t_score values('10003',1,86);
insert into t_score values('10003',2,68);
insert into t_score values('10003',3,58);
insert into t_score values('10003',4,77);
insert into t_score values('10003',5,92);
insert into t_score values('10003',6,89);


insert into t_score values('10004',1,75);
insert into t_score values('10004',2,63);
insert into t_score values('10004',3,87);
insert into t_score values('10004',4,67);
insert into t_score values('10004',5,82);
insert into t_score values('10004',6,96);
--补考
insert into t_score values('10001',1,66);
insert into t_score values('10001',2,61);
insert into t_score values('10001',3,54);
insert into t_score values('10001',3,60);


select * from t_score;

联接查询语句:

select * from  t_student  cross  join  t_course;

不使用CROSS JOIN进行查询:

select * from t_student , t_course;

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/88171166