【Mysql】详解Mysql单表查询

简单查询

查询student表中的所有行和所有列

select * 
from student;

查询student表中sex为women的name列和score列。

select name,score 	//确定列
from student 		//确定表
where sex='woman';	//查询条件

查询student表中score大于80的所有行。

select *
from student 	//确定表
where score>80;	//查询条件
带in关键字的查询

查询student表中id为1001,1002,1003的行

select *
from student 
where id 
in(1001,1002,1003);

查询student表中id不为1001,1002,1003的行

select *
from student 
where id 
not in(1001,1002,1003);

在这里插入图片描述

带between and的范围查询

查询student表中id在1001到1005范围之间的所有行

select *
from student 
where id 
between 1001 and 1005;

在这里插入图片描述
查询student表中id不在1001到1005范围之间的所有行

select *
from student 
where id 
not between 1001 and 1005;
带like的字符串匹配查询

【1】%通配符:可以匹配任意长度的字符串

查询student表中name中姓张的人的信息

select *
from student 
where name like "张%";

查询student表中name中有昭字的人的信息

select *
from student 
where name like "%昭%";

查询student表中name中有姓J以F字结束的人的信息

select *
from student 
where name like "J%F";

在这里插入图片描述
查找student表中name中有%的信息

select *
from student 
where name like "%\%%";
//其中前后两个%代表通配符,中间的\%将%转义成普通字符

在这里插入图片描述
【2】_通配符:一个_只能匹配一个字符
查询student表中name中名字为两个字姓张的人的信息

select *
from student 
where name like "张_";

查询student表中name中名字为三个字且第二个字为W的人的信息

select *
from student 
where name like "_W_";

在这里插入图片描述

空值查询

查询student表中score字段为空的记录

select *
from student 
where score is null;

查询student表中score字段不为空的记录

select *
from student 
where score is not null;
带and的多条件查询

查询student表中sex为women且score大于80的记录

select * 
from student 
where sex='women' 
and socre >80;

查询student表中sex为man且score大于80且age大于20的记录

select * 
from student
where sex='man' 
and score>80 
and age>20;

在这里插入图片描述

带or的多条件查询

只要满足条件中的一个这条记录就会被查出来

查询student表中age大于20或者score大于80的记录

select * 
from student 
where age>20 
or score >80;

在这里插入图片描述
or和and可以混合使用,and的优先级比or的优先级高
查询student表中age大于20或者score大于80且sex为man的记录

select * 
from student 
where age>20 
or score >80 
and sex='man';

相当于

select * 
from student 
where age>20 
or (score >80 and sex='man');

在这里插入图片描述

DISTINCT查询结果不重复

查询student表,消除id字段中重复的记录。

select distinct id 
from student;
对查询结果进行排序

查询student表中sex为man的记录 并按score的降序排列。

select * 
from student 
where sex='man' 
order by score DESC;

查询student表中sex为man的记录 并按score的升序排列。

select * 
from student 
where sex='man' 
order by score ASC;

在这里插入图片描述
注意:如果不指定ASC或者DESC的话,默认是升序排列。

分组查询

group by关键字可以将查询结果按某个字段或者多个字段进行分组,字段中值相同的为一组。
group by通常与集合函数在一起使用。
集合函数包括:count(), sum(), avg(), max(), min()(下文马上讲到)

【1】单独使用group by查询结果只显示结果分组的一条记录。

select *
from student 
group by sex;

查询结果只显示两条记录
这两条记录的sex值分别为一个男的和一个女的。
这说明group by单独使用的时候 只会显示每个分组的一条记录,意义不大
因此,group by 函数只有和集合函数结合起来使用才能发挥出来作用。

【2】group by和group_concat()结合使用

select sex,
group_concat(name) 
from student 
group by sex;

在这里插入图片描述
结果显示,查询结果分为了两组,sex值为 ‘女’ 的分为了一组,值为 ‘男’ 的分为了一组。而且每一组中所有人的名字都被列了出来,达到了我们想要的结果。

【3】group by和having子句的使用

having子句通常与group by 子句一起使用,在完成对分组结果统计后,可以使用having子句对分组的结果做进一步的筛选。如果不使用group by 子句,having子句的功能与where子句一样。

having子句和where子句的相似之处就是都定义搜索条件,但是和where子句不同,having子句与组有关,而where子句与单个的行有关。

select *from student group by name having score>80;

在这里插入图片描述

【4】group by 和集合函数一起使用

select sex,
count(sex) 
from student 
group by sex;

在这里插入图片描述

select sex,
sum(score) 
from student 
group by sex;

在这里插入图片描述

select 
id,
avg(score) 
from student 
group by name;

在这里插入图片描述

用limit限制查询结果的数量

只显示查询的五条记录

select * 
from student 
limit 5;

在这里插入图片描述
设置初始查询位置和要查询的条数.

select * 
from student 
limit 0,6;

从第0条记录开始查询六条。
在这里插入图片描述

select * 
from student 
limit 2,2;

从第2条记录开始查询两条。
在这里插入图片描述

集合查询语句

集合函数包括:
count():求参数的个数的总和
sum():求参数的值的总和
avg():求参数的值的平均值
max():求参数的值的最大值
min():求参数的值的最小值

select 
count(name) 
from student;

在这里插入图片描述

select 
sum(score) 
from student;

在这里插入图片描述

select 
avg(score) 
from student;

在这里插入图片描述

select id,
name,
max(score) 
from student;

在这里插入图片描述

select id,
name,
min(score) 
from student;

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值