mysql数据库高级语句和视图 -上

目录

一、常用查询 

1、按关键字排序

语法

单字段排序

条件查询(结合where)

多字段排序

区间判断及查询不重复记录(and和or)

嵌套/多条件

distinct 查询不重复记录

2、对结果进行分组

语法

分组排序

分组条件

3、限制结果条目(limit)

语法

4、设置别名(alias —as)

语法

列别名设置示

表别名设置

计数列别名 

AS作为连接语句的操作符(主键不复制

5、通配符

6、子查询

不同表/多表嵌套

子查询-exists

二、视图

1、概念

2、作用

3、创建视图

4、NULL 值

总结


一、常用查询 

1、按关键字排序

语法

select column1, column2, ... from table_name order by column1, column2, ... 

单字段排序

显示id,name,score并按分数升序排序,默认不指定是升序排列 asc
select id,name,score from shi order by score ;
显示id,name,score并按分数降序排序,降序为desc
select id,name,score from shi order by score desc;

条件查询(结合where)

where过滤hobby是爱3,并按分数降序排序
select * from shi where hobby='爱3' order by score desc;

多字段排序

查先按分数降序排序,如果分数重复,在按id升序
select * from shi order by score desc,id asc;

区间判断及查询不重复记录(and和or)

查分数小于60或者分数大于85的信息
select * from shi where score <60 or score > 85;

查分数大于60且分数小于85的信息
select * from shi where score >60 and score < 85;

嵌套/多条件

查分数小于60或者分数在80和90之间的
select * from shi where score <60 or (score >80 and score <90 );

distinct 查询不重复记录

select distinct 字段 from 表名﹔
select distinct hobby from shi;

分数和爱好同时重复才会去除
select distinct score,hobby from shi;

1.distinct必须放在最开头
2.distinct只能使用需要去重的字段进行操作

2、对结果进行分组

语法

select column_name, aggregate_function(column_name) from table_name where column_name operator value group by column_name;
select 字段,聚合函数 from 表名,(where字段名(匹配)数值) group by 字段名;
大于60的进行依赖分数分组,同时统计各分组的名字数量
select count(name),score from shi where score >60 group by score;

分组排序

依靠分数分组,统计数量,这个数量按倒叙排列
select count(name),score from shi group by score order by count(name) desc;

分组条件

查分数大于60,依靠分数分组,计数结果按倒叙排列
select count(name),score from shi where score >60 group by score order by count(name) desc;

3、限制结果条目(limit)

语法

SELECT column1, column2, ... FROM table_name LIMIT [offset,] number
查前四行数据(包括表头)
select * from shi limit 3;

查询从三行后开始的两行数据(左开右闭合)
select * from shi limit 3,2;

查按id倒序排列的前三行
select * from shi order by id desc limit 3 ;

4、设置别名(alias —as)

当表的名字比较长或者表内某些字段比较长时,为了方便书写或者 多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增强可读性

语法

对于列的别名:SELECT column_name AS alias_name FROM table_name;
对于表的别名:SELECT column_name(s) FROM table_name AS alias_name;

列别名设置示

把name改成姓名,score改成成绩
select name as 姓名,score as 成绩 from shi;

表别名设置

shi表临时设置表名为i
select i.name as 姓名,i.score as 成绩 from shi as i;

计数列别名 

统计shi表的数量,数量表头为别名number
select count(*) as number from shi;

AS作为连接语句的操作符(主键不复制

创建表shi01,把shi表内容插入shi01
create table shi01 as select * from shi;
或者
create table shi01 (select * from shi);

在此基础可以添加where条件
create table shi01 as select * from shi where score >60;
或者
create table shi01 (select * from shi where score >60);

5、通配符

%:百分号表示零个、一个或多个字符		*
_:下划线表示单个字符 	
查名字以l开头的数据
select * from shi where name like 'l%';

查名字以l结尾的数据
select * from shi where name like '%l';

查名字中间是l的数据
select * from shi where name like '%l%';

查名字以l前面有个字符结尾的数据
select * from shi where name like '_l%';

6、子查询

子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语句

先查询分数大于78的,在筛选id,name的数据(先配括号的字句,在配外面的主句)
select id,name from shi where id in (select id from shi where score >78);

不同表/多表嵌套

先查询表jiu里的id,根据id在筛选shi里面的id和name(子查询的id和主查询id是关系依据)
select id,name from shi where id in (select id from jiu);
把shi表的数据,插入到shi01里面
insert into shi01 select * from shi where id in (select id from shi);
查找shi02表的的id为2的,对应shi里,然后吧分数改为50
update shi set score=50 where score in (select score shi02 where score>72);
删除分数小于80的记录
delete from shi where id not in (select id where score>=80);

子查询-exists

EXISTS 这个关键字在子查询时,主要用于判断exists之后的条件是否成立,如果成立,则正常执行主语句的匹配,如不成立,则不会执行主语句查询

查询分数是80的数据,统计总表数量
select count(*) from shi where exists (select id from shi where score=80);
select a.id from (select id,name from shi) a;

二、视图

1、概念

视图是从一个或多个表中导出来的表,是一种虚拟存在的表

视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据,这样,用户可以不用看到整个数据库中的数据,而之关心对自己有用的数据

数据库中只存放了视图的定义,而没有存放视图中的数据,这些数据存放在原来的表中,使用视图查询数据时,数据库系统会从原来的表中取出对应的数据

视图中的数据依赖于原来表中的数据,一旦表中数据发生改变,显示在视图中的数据也会发生改变

数据库中的虚拟表,这张虚拟表中不包含真实数据,只是做了映射,动态保存结果集(数据)

2、作用

使操作简单化,可以对经常使用的查询定义一个视图,使用户不必为同样的查询操作指定条件

增加数据的安全性,通过视图,用户只能查询和修改指定的数据

提高表的逻辑独立性,视图可以屏蔽原有表结构变化带来的影响

3、创建视图

创建shi02表的视图表,分数大于80的
create view v_score1 as select * from shi02 where score >80;

#在视图表和原表中数据是一致的,如果改修视图表,原表会随之改变
#在修改数据的时候,如果数据值与视图条件不匹配,会消失,如果有新数据满足,它会显示出来

4、NULL 值

null值与空值的区别(空气与真空)

空值长度为0,不占空间,NULL值的长度为null,占用空间
is null无法判断空值
空值使用"=“或者”<>"来处理(!=)
count()计算时,NULL会忽略,空值会加入计算

#统计数量:检测null是否会加入统计中
select count(addr) from shi02;

#不会计入统计的数量中
#查询null值
select * from shi02 where addr is null;
空值数量: 
select count(*) from shi02 where addr is null;

#查询不为空的值
select * from shi02 where addr is not null;
非空值数量: 
select count(*) from shi02 where addr is not null;

总结

我们常用的查询方式有按关键字排序,对结果进行分组排序,限制结果条目,设置别名,通配符,子查询

关键词排序:结合order by 段条件,可以多段,可以结合where条件筛选,dinstinct是去除(常用count计数结合)

对结果进行分组:结合group by 段条件,同样可以结合条件

限制结果条目:limit,如果查看前三行limit 3 ,其实显示的四行(表头),看4,5就是limit 3,2

设置别名:as,就是在表名或者列名 后面加上  as  别名

通配符:%代表多个字符,_代表一个字符

子查询:用in,主句 where 段 in (从句),先匹配从句;主句 where exists (从句),从句成立,才会处理主句

视图:view 视图表名 ,视图是有条件的原表,数据是同步的,但是是建立在满足条件的基础上

null:null不会计入统计内容的数量中,查看可以用is  null(在加count统计null的数量)

Guess you like

Origin blog.csdn.net/y1035793317/article/details/120991551