JavaWeb2 Mysql 基础查询 条件查询 排序查询 分组查询 分页查询

 

 

 基础查询

1 查询表的所有数据   select  * from 表名;

select * from stu;

2 查询指定字段的数据  select 字段列表 from 表名 

select name,age from stu;

3 起别名 As   : as 也可以省略

select name,age as 年龄,english as 英语 from stu;

条件查询

 

 

 查询语句实例

--1  查询年龄大于20岁的学员信息;
select * from stu where age>20;
--2 查询年龄大于等于20岁的学院信息
select * from stu where age>=20;
--3  查询年龄大于等于20岁 并且年龄小鱼等于30岁的学院信息(3种都可以)

select * from stu where  age>=20 and age<=30;

select * from stu where age>=20 && age<=30;

select * from stu where age between 20 and 30;
--4  查询入学日期在'1998-09-01'到'1999-09-01'之间的学院信息
select * from stu where hire_date between '1998-09-01'and'1999-09-01';
--5 查询 年龄等于18的学员信息
select * from stu where age=18;
--6 查询 年龄不等于18的学员信息
select * from stu where age!=18;
--7  查询年龄等于18 或者年龄等于20岁 或者年龄等于22岁的学院信息(3种都可以)
select * from stu where age=18||age=20||age=22;

select * from stu where age=18 or age=20 or age=22;

select * from stu where age in(18,20,22);

--8 查询英语成绩为null的学院信息
--注意:null值的比较不能用=  !=  (需要使用 is  is not)
select * from stu where english is null;

select * from stu where english is not null;

--9  模糊查询 like 占位符  
/* 
(1) _:单个任意字符
(2) %:多个任意字符)
*/
--9.1 查询姓马的学员信息
select * from stu where name like '马%';

--9.2 查询第二个字也是'花'的学员信息
select * from stu where name like '_花%';

--9.3 插叙名字种包含'德'的学院信息
select * from stu where name like '%花%';
View Code

 

排序查询

 

 排序查询案例

/*
排序查询:
语法: select 字段列表 from 表名 order by  排序字段名1 [排序方式1],排序字段名2 [排序方式2]....;
排序方式:
        ASC:升序排列(默认值)
        DESC:降序排列
*/

--1 查询学生信息,按照年龄升序排列
select * from stu order by age asc;
--2 查询学生信息,按照数学成绩降序排列
select * from stu order by math desc;
--3 查询学生信息,按照数学成绩降序排列,如果数学成绩一样,在按英语成绩升序排列
select * from stu order by math desc ,english asc;
View Code

 

分组查询

 

 聚合函数案例应用

/*
*  null值不参与所有聚合函数运算
*  count:统计数量; count 统计的列名不能为空
        *取值:
        1.  主键
        2.  *
*  max :求 最大值;
*  min :求最小值; 
*  sum :求和;
*  avg :求平均值;
*/

--1 统计班级一共多少个学生(两种  建议选列名填*)
select count(id) from stu;

select count(*) from stu;
--2 查询数学成绩的最高分
select max(math) from stu;

--3 查询数学成绩的最低分
select min(math) from stu;

--4 查询数学成绩的总分
select sum(math) from stu;

--5 查询数学成绩的平均分
select avg(math) from stu;

--6 查询英语成绩的最低分
select min(english) from stu;
View Code

 

 

 分组查询

/*
分组函数 
select 字段列表 from 表名 where 分组前条件限定 group by 分组字段名 having 分组后条件过滤

注意:分组之后,查询的字段为聚合函数和分组字段,查询其他字段无任何意义

where 和 having区别
执行时机不一样:where 是分组之前进行限定,不满足where条件,则不参与分组,而having是分组之后对结果进行过滤
可判定的条件不一样:where不能对聚合函数进行判断,having可以.
执行顺序: where>聚合函数>having
*/

--1 查询男同学和女同学各自的数学平均分
select sex ,avg(math) from stu group by sex;
-- 注意: 分组之后,查询的字段为聚合函数和分组字段,查询其他字段无任何意义

--2 查询男同学和女同学各自的数学平均分,以及各自人数
select sex ,avg(math),count(*) from stu group by sex;

--3 查询男同学和女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组 
select sex ,avg(math),count(*) from stu where math>70 group by sex;

--4 查询男同学和女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组,分组之后人物大于两个
select sex ,avg(math),count(*) from stu where math>70 group by sex having count(*)>2;
View Code

分页查询

 

 

 分页查询案例

/*
*分页查询
*
*select 字段列表  from 表名 limit 起始索引 ,查询条数;
*起始索引:从0开始
* 计算公式:起始索引=(当前页码-1)*每页显示的条数
*
*tips:
*分页查询 limit 是MySQL数据库的方言
* Oracle分页查询时hi用的rownumber
* SQL Server分页查询使用top
*/
--1 从0开始查询,查询3条数据;
select * from stu limit 0,3;

--2 每页显示3条数据,查询第1页数据
select * from stu limit 3,3;

--3 每页显示3条数据,查询第2页数据
select * from stu limit 6,3;

--4 每页显示3条数据,查询第3页数据

select * from stu limit 9,3;

--起始索引=(当前页数-1)*每页显示条数
View Code

 

posted @ 2022-07-06 00:09  还有什么值得拥有  阅读(76)  评论(0编辑  收藏  举报