在join中,on和where的區別

2021-10-03 11:53:35 字數 4762 閱讀 4210

1、on條件是在生成臨時表時使用的條件,它不管on中的條件是否為真,都會返回左邊表中的記錄。

2、where條件是在臨時表生成好後,再對臨時表進行過濾的條件。這時已經沒有left  join的含義(必須返回左邊表的記錄)了,條件不為真的就全部過濾掉

建議使用時使用where過濾掉不符合的條件。

select sc.degree,st.sname,c.cname from score sc join course c on sc.cno = c.cno 

left join teacher t on c.tno = t.tno 

left join student st on st.sno = sc.sno 

and t.depart = '計算機系'

where t.depart = '計算機系'

建表語句:

create table student

( sno varchar(3) not null,

sname varchar(4) not null,

s*** varchar(2) not null,

sbirthday datetime,

class varchar(5)

) default charset=utf8;

-- varchar(m) 每個值占用的位元組長度=該值位元組數+1,m只是其最大值--

-- mysql retrieves and displays datetime values in 'yyyy-mm-dd hh:mm:ss' format--

create table course

( cno varchar(5) not null,

cname varchar(10) not null,

tno varchar(3) not null

)default charset=utf8;

create table score

( sno varchar(3) not null,

cno varchar(5) not null,

degree numeric(10, 1) not null

) default charset=utf8;

-- numeric(5,2)的賦值範圍是[-999.99,999.99]--

-- numeric(m)等價於numeric(m,0), numeric等價於numeric(10)--

create table teacher

( tno varchar(3) not null,

tname varchar(4) not null,

t*** varchar(2) not null,

tbirthday datetime not null,

prof varchar(6),

depart varchar(10) not null

)default charset=utf8;

alter table student add primary key (sno);

alter table score add primary key (sno,cno);

alter table course add primary key (cno);

alter table teacher add primary key (tno);

-- 先新增外來鍵,再新增值--

-- 外來鍵在兩張table中的資料型別須一致--

alter table score add constraint fk_score_student foreign key (sno) references student(sno);

alter table score add constraint fk_score_course foreign key (cno) references course(cno);

alter table course add constraint fk_course_teacher foreign key (tno) references teacher(tno);

-- 先對references中所指的表新增值--

insert into student (sno,sname,s***,sbirthday,class)

values (108 ,'曾華' ,'男' ,'1977-09-01',95033);

insert into student (sno,sname,s***,sbirthday,class)

values (105 ,'匡明' ,'男' ,'1975-10-02',95031);

insert into student (sno,sname,s***,sbirthday,class)

values (107 ,'王麗' ,'女' ,'1976-01-23',95033);

insert into student (sno,sname,s***,sbirthday,class)

values (101 ,'李軍' ,'男' ,'1976-02-20',95033);

insert into student (sno,sname,s***,sbirthday,class)

values (109 ,'王芳' ,'女' ,'1975-02-10',95031);

insert into student (sno,sname,s***,sbirthday,class)

values (103 ,'陸君' ,'男' ,'1974-06-03',95031);

insert into student (sno,sname,s***,sbirthday,class)

values (110 ,'wkb' ,'男' ,'1974-06-03',95031);

insert into teacher(tno,tname,t***,tbirthday,prof,depart)

values (804,'李誠','男','1958-12-02','副教授','計算機系');

insert into teacher(tno,tname,t***,tbirthday,prof,depart)

values (856,'張旭','男','1969-03-12','講師','電子工程系');

insert into teacher(tno,tname,t***,tbirthday,prof,depart)

values (825,'王萍','女','1972-05-05','助教','計算機系');

insert into teacher(tno,tname,t***,tbirthday,prof,depart)

values (831,'劉冰','女','1977-08-14','助教','電子工程系');

insert into course(cno,cname,tno)values ('3-105' ,'計算機導論',825);

insert into course(cno,cname,tno)values ('3-245' ,'作業系統' ,804);

insert into course(cno,cname,tno)values ('6-166' ,'資料電路' ,856);

insert into course(cno,cname,tno)values ('9-888' ,'高等數學' ,831);

insert into score(sno,cno,degree)values (103,'3-245',86);

insert into score(sno,cno,degree)values (105,'3-245',75);

insert into score(sno,cno,degree)values (109,'3-245',68);

insert into score(sno,cno,degree)values (103,'3-105',92);

insert into score(sno,cno,degree)values (105,'3-105',88);

insert into score(sno,cno,degree)values (109,'3-105',76);

insert into score(sno,cno,degree)values (101,'3-105',64);

insert into score(sno,cno,degree)values (107,'3-105',91);

insert into score(sno,cno,degree)values (108,'3-105',78);

insert into score(sno,cno,degree)values (101,'6-166',85);

insert into score(sno,cno,degree)values (107,'6-166',79);

insert into score(sno,cno,degree)values (108,'6-166',81);

join中的條件在on還是在where中?

left join 左連線,返回左表中所有的記錄以及右表中連線字段相等的記錄。right join 右連線,返回右表中所有的記錄以及左表中連線字段相等的記錄。inner join 內連線,又叫等值連線,只返回兩個表中連線字段相等的行。full join 外連線,返回兩個表中的行 left join ...

SQL中的Join和Where的區別

left join 左連線,返回左表中所有的記錄以及右表中連線字段相等的記錄。right join 右連線,返回右表中所有的記錄以及左表中連線字段相等的記錄。inner join 內連線,又叫等值連線,只返回兩個表中連線字段相等的行。full join 外連線,返回兩個表中的行 left join ...

SQL中的Join和Where的區別

left join 左連線,返回左表中所有的記錄以及右表中連線字段相等的記錄。right join 右連線,返回右表中所有的記錄以及左表中連線字段相等的記錄。inner join 內連線,又叫等值連線,只返回兩個表中連線字段相等的行。full join 外連線,返回兩個表中的行 left join ...