MySQL----DQL(查询数据库表中数据)

##DQL:查询表中的记录

一、语法:

    select  数据库

      字段列名  函数

    from  spa

      表名列表  code

    where  blog

      条件列表  排序

    group  by  索引

      分组字段  数学

    having  分组以后的条件  it

    order  bytable

      排序

    limit

      分页限定

二、基础查询

  一、多个字段的查询

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

    *注意:

      * 若是查询全部字段,则可使用*来代替字段列表。

  二、去除重复

    * distinct

  三、计算列

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

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

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

      表达式2:若是该字段为null后的替换值

  四、起别名

    *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;

 三、条件查询

  一、where子句后跟条件

  二、运算符

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

    *  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 '%马%';

 四、排序查询

  *  语法: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 ;

 

五、聚合函数:将一列数据做为总体,进行葱纵向计算

  1、count:计算个数

    一、通常选择非空的列:主键

    二、count(*)

  二、max:计算最大值

  三、min:计算最小值

  四、sum:计算和

  五、avg:计算平均值

 

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

    *  解决方案:

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

      二、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;

 

六、分组查询

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

  二、注意:

    一、分组以后查询的字段:分组字段、聚合函数

    二、where 和 having 的区别

      一、where在分组以前进行限定,若是不知足条件,则不参与分组。Having在分组以后进行限定,若是不知足结果则不会被查询出来。

      二、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;

 

七、分页查询

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

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

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

/*每页显示3条记录*/
select * from student limit 0,3;  /*第一页*/
select * from student limit 3,3;  /*第二页*/
相关文章
相关标签/搜索