学习笔记(十五)——MySQL(查询)
发布日期:2021-05-07 07:21:02 浏览次数:21 分类:精选文章

本文共 3039 字,大约阅读时间需要 10 分钟。

查询方法

下面的查询都是对单表的查询,所以先创建表tb1(下面的tb1均代表此表)

create table tb1(id int,name varchar(10),age int,class varchar(5));   #创建tb1表

对于MySQL的创建、插入等基本语句有不理解的可以看我上一篇博客:


1、根据条件查询

select * from tb1 where age=19;    #查询年龄所有等于19岁select * from tb1 where age>19;   #查询年龄所有大于19岁select * from tb1 where age>=19;   #查询年龄所有大于等于19岁select * from tb1 where class!='two';   #查询班级不为two的

1. is null和is not null

select * from tb1 where class is null;   #查询班级为空select * from tb1 where class is not null;  #查询班级非空

2.and、or 、not

select * from tb1 where age=19 and class='two';    #and 和 所有条件都要满足select * from tb1 where age=19 or class='two';     #or 或者 满足其中一个条件select * from tb1 where not age=19;              # not 非 即条件的否定 查询年龄不等于19select * from tb1 where age!=19;                 #查询年龄不等于19

2、排序

关键字:asc、desc

select * from tb1 order by age;   #默认升序select * from tb1 order by age asc;  #升序select * from tb1 order by age desc;  #降序

3、限制

limit m,n 从下标为m的行数开始显示n条数据

select *from tb1 limit 3;    #只显示3行,即前三行select *from tb1 limit 3,3;  #显示的是从第4行开始,显示3行select *from tb1 order by age desc limit 3;  #取年龄最高的前三名

4、去重

注意关键字distinct

select distinct * from tb1;   #去掉有重复的数据

5、模糊查询

select * from tb1 where name like '%';    % 代表任意多个字符select * from tb1 where name like 'zhang%';    查询name以zhang开头的select * from tb1 where name like 'zhang___';    _ 代表一个任意字符  3个_代表三个字符

6、范围查询

关键字:between···and

select * from tb1 where age between 19 and 22;   #查询年龄在19-20岁select * from tb1 where 19<=age<=22;    #查询年龄在19-20岁select * from tb1 where class in ('one','three');  #查询班级为one和three的

7、聚合函数

select max(age) from tb1;    #求最大值,这里值年龄最大值select min(name) from tb1;    #求最小值 ,字符串按照26个字母排序select count(name) from tb1;    #统计字段值不为空的个数select count(*) from tb1;   #计算数据总数#注意:count 可以写*select avg(age) from tb1;    #求平均值select sum(age) from tb1;    #求和select group_concat(name) from tb1;   #列出字段所有的值

8、分组查询

分组查询通常和聚合函数一起使用。

在分组情况下,只能出现分组字段和聚合字段,其他字段没有意义,会报错。

select class from tb1 group by class;   #根据class分组select class,count(name) from tb1 group by class;   #计算每个年级的人数select class,avg(age) from tb1 group by class;   #计算每个年级的平均人数

9、聚合筛选

select class,count(name) from tb1 group by class having count(name)=5;  筛选count(name)=5select class,count(name) from tb1 group by class having class='two';   筛选class='two'

10、子查询

select * from tb1 where age>(select avg(age) from tb1);  #括号不能少    查找年龄大于平均年龄的

内连接与外连接

内连接与外连接是针对多表操作的,所以这里先创建表tb2和tb3。(下面的tb2和tb3都是代表这两个表)

create table tb2(    #创建表tb2id int,name varchar(20),gender enum('0','1'),join_date datetime,dept_id int);create table tb3(  #创建表tb3dept_id int,dept_name varchar(20),salary int);

1、内连接

指连接结果仅包含符合连接条件的行,参与连接的两个表都应该符合连接条件。

基本语法:

左表 [inner] join 右表         on 左表.字段 = 右表.字段;

1. 无条件内连接

select * from tb2 inner join tb3;   #不推荐,假设tb2有10条数据,tb3有5条,无条件连接后就有50条

2.有条件内连接

select * from tb2 inner join tb3 on tb2.dept_id=tb3.dept_id;

2、外连接

连接结果不仅包含符合连接条件的行同时也包含自身不符合条件的行。

1.左外连接

左表的全部展示出来 右边只会显示符合搜索条件的。

select * from tb2 left join tb3 on tb2.dept_id=tb3.dept_id;

2.右外连接

右边表数据行全部保留,左边表保留符合连接条件的行。

select * from tb2 right join tb3 on tb2.dept_id=tb3.dept_id;
上一篇:蓝桥杯-模拟正整数序列的数量
下一篇:即日起,博客双平台同步发布

发表评论

最新留言

不错!
[***.144.177.141]2025年04月15日 01时43分53秒