联合查询、子查询、视图

-- 联合查询

select * from my_class

union -- 默认去重

select * from my_class;

select * from my_class

union all -- 不去重

select * from my_class;

select id,c_name,room from my_class

union all -- 不去重

select name,number,idfrom my_student;

-- 需求:男生升序,女生降序(年龄)(

select * from my_studentwhere sex='男' order by age asc limit 9999999)

union(

select *from my_studentwhere sex='女' order by agedesc limit 9999999);

-- 标量子查询

select * from my_studentwhere c_id=(select id from my_classwhere name='python1910');

-- 列子查询

select * from my_studentwhere c_idin (select id from my_class);

-- any some all

select * from my_studentwhere c_id =any (select id from my_class);

select * from my_studentwhere c_id =some (select id from my_class);

select * from my_studentwhere c_id =all (select id from my_class);

select * from my_studentwhere c_id !=any (select id from my_class);-- 所有结果(null除外)

select * from my_studentwhere c_id !=some (select id from my_class);-- 所有结果(null除外)

select * from my_studentwhere c_id !=all (select id from my_class);-- 1,2不等于345(null除外)

select * from my_studentwhere age=(select max(age)from my_student)and height=(select max(height)from my_student);

-- 行子查询

select * from my_studentwhere (age,height)= (select max(age),max(height)from my_student);

select *from my_studentorder by age desc,heightdesc limit 1;-- 可能查询结果不是预想的

-- 表子查询

select * from my_studentgroup by c_idorder by heightdesc;

-- 插入学生

insert into my_studentvalues (null,'bc20200007','小红','女',25,185,1);

-- 查找每个班身高最高的学生

select * from (select * from my_studentorder by heightdesc limit 9999999)as studentgroup by c_id;


--

select exists(select * from my_student);

select exists(select * from my_studentwhere id=100);

-- exists子查询

select * from my_studentwhere  exists(select * from my_class);-- 判断括号里是否成立

select * from my_studentwhere  exists(select * from my_classwhere id=3);

select * from my_studentwhere  exists(select * from my_classwhere id=2);

-- 视图:单表+多表

create view my_v1as select *  from my_student;

create view my_v2as select *  from my_class;

create view my_v3as select *  from my_studentas sleft join my_classas con s.c_id=c.id;-- 错误:id重复

-- 多表视图

create view my_v3as select s.*,c.name as c_name,c.room from my_studentas sjoin my_classas con s.c_id=c.id;

show tables;

-- 查看视图创建语句

show create view my_v3\G

-- 视图的使用

select *  from my_v1;

select *  from my_v2;

select *  from my_v3;

-- 修改视图

alter view my_v1as select id,name,age,sex,height,c_idfrom my_student;

-- 删除视图

create view my_v4as select * from my_student;

drop view my_v4;

-- 多表视图插入数据 : 不能插入数据

insert into my_v3values (null,'bc20200008','张三丰','男',150,180,3,'python1910','a204');

-- 将学生表的学号字段设置成不能为空

alter table my_studentmodify number char(10)not null unique ;

-- 单表视图插入数据: 视图不包括所有不允许为空的字段

insert into my_v1values (null,'bc20200008','张三丰',150,'男',180,3);

-- 单表视图插入数据

insert into my_v2values (2,'python1903','a204');

-- 多表视图删除数据: 不能删除数据

delete from mydb.my_v3where id=1;

-- 单表视图删除数据

delete from my_v2where id=4;

-- 多表视图更新数据

update my_v3set c_id=4 where id=6;

-- 视图: age 字段限制更新

create view my_v4as select * from my_studentwhere age>30 with check option;

-- 表示视图的数据来源都是年龄大于30岁的,是由where age>30 决定的

-- with check option决定通过视力更新的时候,不能将已经得到的数据age>30的改成<30的

-- 将视图可以查看到的数据改成年龄小于30

update my_v4set age=29 where id=5;

-- 可以修改数据让视图可以查到: 可以改,但是没有效果

update my_v4set age=32 where id=3;

你可能感兴趣的:(联合查询、子查询、视图)