Mysql查询语句全教程(文末附SQL脚本)

(一)查询语句基本语法

所谓查询就是一列一列进行查询

select
	字段列表
from
	表名
where
	条件列表
group by
	分组字段
having
	分组之后的条件
order by
	排序
limit
	分页限定

(二)基本查询语句

select name,age from student3;

(三)去重查询

select DISTINCT address from student3;

另外有点需要注意的事情,当你使用去重查询以后,还是出现相同的字段,说明可能你当时插入的时候转行了,涉及到了空格了

去重的话,必须结果集完全一样才可以进行去重;

(四)查询英语数学,还有英语数学的成绩总和

SELECT name ,math,english,math+english from student3 ;

但是会出现一种问题如果其中一科的成绩是null的话,那么这个人的成绩总和也是null;显然这是不对的,例如考试时候,有人这一科目因为个人原因没有参加,难道最后给人家个0分总和?

我们可以使用ifnull来进行解决

select name,math,english,math+IFNULL(english,0) from student3;

(五)别名设置

select name,math,english,math+IFNULL(english,0) as 总分 from student3;

(六)条件查询

条件查询
	1. where子句后跟条件
	2. 运算符
	* > 、< 、<= 、>= 、= 、<>
	* BETWEEN...AND  
	* IN( 集合) 
	* LIKE:模糊查询
	* 占位符:
	* _:单个任意字符
	* %:多个任意字符
	* IS NULL  
	* and  或 &&
	* or  或 || 
	* not  或 !

(七)运算符的使用

查询年龄等于18,20,22的情况
select * from student3 where age=18 or age=20 or age=22;
select * from student3 where age in (18,20,22);
查询年龄大于20
select * from student3 where age >20;

查询年龄不等于55的
select * from student3 WHERE age<>55;
select * from student3 WHERE age!=55;
两种写法是一样的

查询年龄大于等于20,并且年龄小于等于30
select * from student3 where age>=20 and age<=30;
select * from student3 where age>=20 && age<=30;(不推荐使用);
select * from student3 where age between 20 and 30;(包括20,同时也包括三十);

查询年龄等于18,20,22的情况
select * from student3 where age=18 or age=20 or age=22; (or还可以使用||来进行替代)
select * from student3 where age in (18,20,22);

查询英文成绩为null的人的情况
select * from student3 where english is null;
select * from student3 where english = null;(不能使用=来对null值进行判断,同样!=也不可以)
select * from student3 where english is not null;(查询英语成绩不为null的情况)

(八)模糊查询

首先来学习一下占位符
%多个任意字符
_单个任意字符

查询姓马的有哪些
select * FROM student3 WHERE name like '%马%';
查询姓名第二个字是化的人
select * from student3 WHERE name like '_化%';
查询姓名是三个字的人
select * from student3 where name like '___';
查询姓名中包含德字的人
select * from student3 where name like '%德%';

(九)排序查询

排序查询
ORDER BY 子句 ;        ORDER BY 排序字段 排序方式;
排序方式: ASC 升序 DESC 降序
注意:有多个排序条件的时候,则当前边的条件值一样时,才会判断第二条件

排序数学成绩
select * from student3 ORDER BY math ;默认为升序排列(成绩的数值越来越大)
select * from student3 ORDER BY math DESC;这是降序排列(成绩的数值越来越小)
当数学成绩一样时候,按照英语成绩降序排列
select * from student3 ORDER BY math desc ,english desc;

(十)聚合函数

聚合函数
将一列数据作为一个整体,进行纵向的计算
count:计算个数  1.一般选择非空的列:主键 2.count(*)
max:计算最大值
min:计算最小值
sum:计算总和值
avg:计算平均值
注意:聚合函数的计算,排除null值 
解决方案:1.选择非空的列进行计算 2.ifnull
查询表中一共有多少人
select count(*) from student3;
select count(english) from student3;(这种查询就少了一个,因为有一人的英语成绩为null,所以我们需要查询不为空的列,例如主键)

查询数学成绩的最高分
select max(math) from student3;
查询数学成绩的最低分
select min(math) from student3;
计算数学成绩的平均值
select avg(math) from student3;
计算数学成绩的总和
select sum(math) from student3;

(十一)分组查询

分组查询
group by 分组字段
1.注意分组之后查询的字段:分组字段 聚合函数 (写其他的字段没有任何意义)
2.where和having的区别
①where在分组之前进行限定,如果不满足条件,则不参与分组;having在分组之后进行限定,如果不满足条件,则不会被查询出来
②where后不可以跟聚合函数,having可以进行聚合函数的判断

按照性别分组,分别查询男女同学的平均分
select sex,avg(english) from student3 GROUP BY sex;
按照性别分组,分别查询男女同学的平均分,以及人数
select sex,avg(math),count(id) from student3 GROUP BY SEX;
对分数低于70分的人不做统计 
select sex,avg(math),count(id) from student3 where math>70 group by sex;
(直接在后边加一个where条件)
查询人数大于两个人的分组
select sex,avg(math),count(id) from student3 where math>70 group by sex having count(id)>2;
另外我们还可以起个别名这样会更好看一些
select sex,avg(math),count(id) 人数 from student3 where math>70 group by sex having 人数>2;

(十二)分页查询

分页查询
select * from student3 LIMIT 0,3;
公式:开始的索引=(当前页数-1)*每页显示的条数

分页查询是一个'方言' mysqL中用的是limit oracle中用的是rownum 

sql脚本 复制进去,直接执行即可

CREATE TABLE student3 ( id int,  name varchar(20),  age int,  sex varchar(5),  address varchar(100),  math int,  english int  ); INSERT INTO student3(id,NAME,age,sex,address,math,english) VALUES (1,'马云',55,'男','杭州',66,78),(2,'马化腾',45,'女','深圳',98,87),(3,'马景涛',55,'男','香港',56,77),(4,'柳岩',20,'女','湖南',76,65),(5,'柳青',20,'男','湖南',86,NULL),(6,'刘德华',57,'男','香港',99,99),(7,'马德',22,'女','香港',99,99),(8,'德玛西亚',18,'男','南京',56,65);

发布了36 篇原创文章 · 获赞 3 · 访问量 5934

猜你喜欢

转载自blog.csdn.net/qq_45014905/article/details/105723574