首页 技术 正文
技术 2022年11月21日
0 收藏 692 点赞 4,206 浏览 3504 个字

##DQL:查询表中的记录

1、语法:

    select

      字段列名

    from

      表名列表

    where

      条件列表

    group  by

      分组字段

    having  分组之后的条件

    order  by

      排序

    limit

      分页限定

2、基础查询

  1、多个字段的查询

    select  字段名1,字段名2,…from 表名;

    *注意:

      * 如果查询所有字段,则可以使用*来代替字段列表。

  2、去除重复

    * distinct

  3、计算列

    * 一般可以使用四则运算计算一些列的值。(一般只会进行数值型的计算)

    * ifnull(表达式1,表达式2):null 参与的运算,计算结果都为null

      表达式1:哪个字段需要去判断是否为null

      表达式2:如果该字段为null后的替换值

  4、起别名

    *as:as也可以省略

  例子:

/*在 lxy数据库中创建一张学生表*/
create table student(
id int,
name varchar(20),
age int,
sex varchar(5),
address varchar(100),
math int,
english int
);
/*插入数据*/
insert into student(id,name,age,sex,address,math,english) values
(1,'马云',66,'男','杭州',100,100);
insert into student(id,name,age,sex,address,math,english) values
(8,'马德',35,'男','香港',78,88);
select * from student ;
/*数据去重*/
select distinct address from student;
/*计算分数之和*/
select name,math,english,math+english from student;
/*如果有Null 参与的运算,计算结果都为Null*/
select name,math,english,math+ifnull(english,0) from student;
/*起别名*/
select name,math,english,math+ifnull(english,0) as Total from student;

3、条件查询

  1、where子句后跟条件

  2、运算符

    *  >;<;<=;>=;=;<>(这个是不等于)

    *  BETWEEN…AND

    * IN(集合)

    *  LIKE

      *  _:单个占位字符

      *  %:多个任意字符

    *  IS NULL

    *  and  或  &&

    *  or  或  ||

    *  not  或  !

  例子1:普通查询

/*查询年龄大于20岁*/
select * from student where age >=30;
/*查询年龄不等于30*/
select * from student where age <> 30;
/*查询年龄在30-50之间*/
select * from student where age between 30 and 50;
select * from student where age >= 30 && age <= 50;
select * from student where age >= 30 and age <= 50;
/*查询年龄为45,35,46的信息*/
select * from student where age = 45 or age = 35 or age = 46;
select * from student where age in(45,35,46);
/*查询英语成绩为null*/
/*这样查是不正确的,null不能使用 =(!=)判断*/
select * from student where english = null;
/*正确写法*/
select * from student where english is null;

  例子2:模糊查询

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

4、排序查询

  *  语法:order by 子句

    *  order by 排序字段1 排序方式1,排序字段2 排序方式2…

  *  排序方式:

    *  ASC:升序,默认的

    *  DESC:降序

  *  注意:

    *  如果有多个排序体哦阿健,则当前边的条件值一样时,才会判断第二条件。

/*按照数学成绩排序 降序 */
select * from student order by math desc ;
/*按照数学成绩排序 降序 如果数学成绩一样 按照英语成绩降序排序*/
select * from student order by math desc,english desc ;

5、聚合函数:将一列数据作为整体,进行葱纵向计算

  、count:计算个数

    1、一般选择非空的列:主键

    2、count(*)

  2、max:计算最大值

  3、min:计算最小值

  4、sum:计算和

  5、avg:计算平均值

  *  注意聚合函数的计算会排除null值。

    *  解决方案:

      1、选择不包含非空的列进行计算。

      2、IFNULL函数

/*计算name的记录条数*/
select count(name) from student;
/* 注意聚合函数的计算会排除null值,解决方案:*/
select count(ifnull(english,0)) from student;
select count(*) from student;
/*计算数学成绩最大值,最小值同理*/
select max(math) from student;
/*求和*/
select sum(math) from student;
/*平均值*/
select avg(math) from student;

6、分组查询

  1、语法:group by 分组字段;

  2、注意:

    1、分组之后查询的字段:分组字段、聚合函数

    2、where 和 having 的区别:

      1、where在分组之前进行限定,如果不满足条件,则不参与分组。Having在分组之后进行限定,如果不满足结果则不会被查询出来。

      2、where后不可以跟聚合函数,having可以进行聚合函数的判断。

/*按照性别分组,分别查询男、女同学的数学平均分*/
select sex, avg(math) from student group by student.sex;
/*按照性别分组,分别查询男、女同学的数学平均分,分别的人数*/
select sex, avg(math),count(id) from student group by student.sex;
/*按照性别分组,分别查询男、女同学的数学平均分,分别的人数 要求:分数低于70分的人不参与分组*/
select sex, avg(math),count(id) from student where math > 70 group by student.sex;
/*按照性别分组,分别查询男、女同学的数学平均分,分别的人数 要求:分数低于70分的人不参与分组 分组之后人数大于2个人*/
select sex, avg(math),count(id) from student where math > 70 group by student.sex having count(id) > 2;
select sex, avg(math),count(id) 人数 from student where math > 70 group by student.sex having 人数 > 2;

7、分页查询

    1、语法:limit   开始的索引,每页查询的条数;

    2、公式:开始的索引 = (当前的页码 – 1) * 每页显示的条数

    3、limit是一个MySQL的”方言“

/*每页显示3条记录*/
select * from student limit 0,3; /*第一页*/
select * from student limit 3,3; /*第二页*/
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,074
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892