mysql高阶语句:

mysql高阶语句:

高级语法的查询语句:

select * from  表名   where 
                     limits
                     distinct   去重查询
                     like       模糊查询

排序语法:关键字排序

升序和降序

默认的排序方式就是升序

升序:ASC 配合order by语法

select * from 表名 order by 列名 asc; 

降序:DESC 配合order by语法

select * from 表名 order by 列名 desc;

以多个关键字进行排序:

select * from info order by hobbid desc,id;

*以多个列作为排序关键字,只有第一个参数有相同的值,第二个字段才有意义。

where条件的筛选功能(比较符号)

区间判断:

and or

select * from info WHERE score > 70 and score <=90;
select * from info where score > 70 or score <=60;

嵌套多条件:

select * from info where score > 70 or (score > 0 and score < 60);

分组查询,sql查询的结果进行分过,使用group by 语句来实现。

group by 语句配合聚合函数一起。

聚合函数的类型:

统计 count

求和 sum

求平均数 avg

最大值 max

最小值 min

select count(name),hobbid from info group by hobbid;

在聚合函数分组语句中,非聚合函数列不一定需要group by,但group by一定有非聚合函数列。

select count(name),hobbid,score from info where score >=80 group by hobbid;
select count(name),hobbid,score from info  group by hobbid having score >=80;

以兴趣这一列作为分组,计算成绩的平均分,统计的结果筛选出分组的平均成绩大于等于60分。

select avg(score),hobbid from info group by hobbid having avg(score) >=60;

统计姓名,以兴趣和分数作为分组,统计出成绩大于80的,然后按照降序,对统计姓名的列进行排序。

select count(name),hobbid,score from info  group by hobbid,score  having score >80 order by name desc;

limit   1,3         #1是位置偏移量(可选参数),如果不设定位置偏移量,默认就从第一行开始,默认的值 0 

使用limit和降序排列,只显示最后三行

select * from info order by id desc limit 3;

表和列的别名:因为在实际工作中,表的名字和列的名字可能会很长,书写起来不太方便,多次声明表和列时,完整的展示太复杂,设置别名可以使书写简化了

可读性增加了,简介明了。

对列名起别名:

select name as 姓名,score as 成绩 from info;
select name 姓名,score 成绩 from info;
#as是可以不加的

对表名起别名:

select i.name 姓名,i.score 成绩 from info i;
select i.name 姓名,i.score 成绩 from info as i;

对表进行复制:

create table test as select * from info;
create table test1 as select * from info where score >= 60;

通配符:

like 模糊查询

%:表示零个,一个或者多个字符 *

_ :表示单个字符。

select * from info where address like 's%';
select * from info where address like 's_';

子查询:(内查询,嵌套查询)

select语句当中又嵌套了一个select

嵌套的select才是子查询,先执行子查询的语句,外部的select再根据子条件的结果进行过滤查找。

子查询可以是多个表,也可以是同一张表。

关联语句 in

select id,name,score from info where id in ( select id from info where score >=80);
select id,name,score from info where id in ( select id from test1 where score >=80);
update info set score = 80 where id in (select id from test where id = 4 );

not in

select id,name,score from info where id not in ( select id from info where score >=80);
select id,name,score from info where id not in ( select id from test1 where score >=80);

exists 判断子查询的结果是否为空,不为空返回true,空返回false

select count(*) from info where exists (select id from test1 where score >80);
#这里不是in和not in 会传给主表。
这里是判断条件,存在才执行,不存在,结果为空则不执行。

查询分数,如果分数小于50的则统计info的字段数。

select count(*) from info where score in (select score from info where score < 50);

在子查询当中多表查询和别名:

info表和test表,这两张表id部分相同。然后根据id相同的部分,查询info表的id的值。

select * from info where id in (select id from test id);

info表和test表,这两张表id部分相同。然后根据id相同的部分,查询info表的id的值,查询出info表成绩大于80的数据。

select id,score from info where score > 80 and id in (select id from test id);

查询info表的平均成绩。

select avg(score) from info where score > 80 and id  in (select id from test id);

mysql的视图:

视图是一个虚拟表,表的数据基于查询的结果生成。

视图可以简化复杂的查询,隐藏复杂的细节。访问数据更安全。

视图表是多表数据的结合体。

视图和表之间的区别:

1、存储方式,表是实际的数据行,视图不存储数据,仅仅是查询结果的虚拟表

2、数据更新,更新表可以直接更新视图表的数据。

3、占用空间,表实际占用空间,视图不占用空间,只是一个动态结果的展示。

视图表的数据可能是一张表的部分查询数据,也可能是多个表的一部分查询数据。

查询当前数据库中的视图表:

show full tables in xy102 where table_type like 'VIEW';

创建视图表

create view test2 as select * from info where score >= 80;

查看视图表中的数据

select * from test2;

创建一张视图表,视图表包含 id name address ,从info和test当中的name值相同的部分创建。

create view test3 as select id,name,address from info where name in (select name from test name);
select * from test3;

视图表就是查询语句的别名,有了视图表可以简化查询的语句。

表的权限是不一样的,库的权限是有控制的。所以查询视图表的权限相对低

既可以保证原表的数据安全,也简化了查询的过程。

删除视图表:

drop view 视图表名;

连接查询:

两张表或者多个表的记录结合起来,基于这些表共同的字段,进行数据的拼接。

首选,要确定一个主表作为结果集,然后把其他表的行有选择性的选定到主表的结果上。

内连接:两张表或者多张表之间符合条件的数据记录的集合。

select a.a_id,a.a_name from test1 a inner join test2 b on a.a_name=b.b_name;

取两个表或者多个表之间的交集。

左连接:左外连接,left jion left outer join

左连接以左表为基础,接收左表的所有行,以左表的记录和右表的记录进行匹配。

匹配左表的所有,以及右表中符合条件的行,不符合的显示null。

select * from test1 left join test2 on a_name=b_name; 

以比较条件为标准,展示结果。两个表相同的部分展示出来,做拼接。不同的结果显示null。

右连接:右外连接,right jion right outer join

左连接以左表为基础,接收左表的所有行,以左表的记录和右表的记录进行匹配。

匹配左表的所有,以及右表中符合条件的行,不符合的显示null。

select * from test1 RIGHT join test2 on a_name=b_name; 

以比较条件为标准,展示结果。两个表相同的部分展示出来,做拼接。不同的结果显示null。

  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值