sql练习
可以将查询结果看成是一个张,
- 子查询-查出两张表,再作连接
- 当前表可以连接当表,看作两张表。进行连接查询
create table stu
(
id int,
name varchar(20),
subject varchar(20),
score double
)
insert into stu values(1,'jack','cn',100);
insert into stu values(1,'jack','en',90);
insert into stu values(2,'rose','cn',70);
insert into stu values(2,'rose','en',90);
insert into stu values(3,'tony','cn',80);
insert into stu values(3,'tony','en',96);
# 表1 cn
select * from stu where subject = 'cn';
# 表2 en
select * from stu where subject = 'en';
# 子查询
select t1.id,t1.name,t1.score 'cn',t2.score 'en'
from
(select * from stu where subject = 'cn') as t1,
(select * from stu where subject = 'en') as t2
where 1=1
and t1.id= t2.id
and t1.score > t2.score
#
select A.id,A.name,A.score 'cn',B.score 'en'
from stu A
inner join stu B
on A.id = B.id
where A.subject = 'cn' and B.subject='en'
and A.score > B.score