DQL mysql

## DQL:查询表中的记录

        * select * from 表名;

        1.  语法:

                select

                         字段列表

                 from

                         表名列表

                 where

                          条件列表

                group by

                          分组字段

                having

                        分组之后的条件

                order  by

                         排序

                limit

                         分页限定

         2.  基础查询:

                1.  多个字段的查询

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

                           select nameage  from student

                           *注意:

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

                                    *select * from student(表名);

                 2. 去除重复:

                            *distinct

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

                 3. 计算列

                           * 一般可以使用四则运算计算一些列的值。(一般只会今次那个数值型的计算)

                                       select 字段1  +  字段2  from 表名;

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

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

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

                                             如果有null参与的运算 ,计算结果都为null

                                             select namemathenglishmath+ifenglish0from student

                 4. 起别名:

                           *as as也可以省略

                                             select namemath  as)数学,english 英语,math+ifenglish

                                              0as 总分 from student

      3. 条件查询

              1. where字句后跟条件

              2. 运算符

                      * > , < , <= , >= , = , <>

                           - - 查询年龄等于20

                           select * from studen where age = 20;

                           - -查询年龄不等于20

                           select * from studen where age = 20;

                           select * from student where age <>20

                            - -查询年龄大于等于20  小于等于30

                           select * from student  where age >=20 AND(&&) age <=30;

                           select *from student where age between 20 and 30;

                           - -查询年龄22 岁,18岁,25岁的信息

                           select * from student where age=22or age=18 or age=25

                           select * from student where age in2218,25);

                           - -查询英语成绩为null

                           select * from student where english = null- -不对的,null值不能使用=(!=)判

                           断;

                           select * from student where english is null

                           - -查询英语成绩不为null

                           select * from student where english is not null;

                      * BETWEEN ...AND

                      *IN(集合)

                      * LIKE:模糊查询

                               * 占位符:

                                       * _:单个任意字节

                                       * %:多个任意字节

                               --查询姓马的有哪些?

                               select * from student where name like '%'

                               --查询姓名第二个字是化的人

                               select * from student where name lile "_%" ;

                               --查询姓名是三个字的人

                               select * from student where name like "___"(三个下滑线);

                               --查询姓名中包含德的人

                               select * from student where name like "%%“

 

 

转义字符:

select * from student where name like '%a%%' escape 'a';

a会面的字符会转化为正常字符         例如:王a%%

 

 

                      *IS NULL

                      *and &&

                      *or ||

                      *not 或!              

            DQL:查询语句

1.排序查询

*语法:order by 子句

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

 

*排序方式:

*ASC:升序,默认的。

*DESC:降序。

 

*注意:

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

2.聚合函数:将一列数据作为一个整体,进行纵向的计算。

1.count:计算个数  (SELECT COUNT(NAME) FROM db1;

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

2.count*)不推荐使用

2.max:计算最大值(SELECT MAX(math) FROM db1;)

3.min:计算最小值(SELECT MIN(math) FROM db1;)

4.sum:计算和(SELECT SUM(math) FROM db1;)[注意:排除null ]

5.avg:计算平均值

 

* 注意:聚合函数的计算,排除null

解决方案:

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

2.IFNULL的函数(SELECT COUNT (IF NULL(ENGLISH,0))FROM DB1)

 

      3.分组查询:

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

2. 注意:

1.分组之后查询的字符:分组字符,聚合字符

2. where having 的区别?

1. where 在分组之前进行限定,如果不满足条件,则不参与分组

     having 在分组之后进行限定,如果不满足结果,则不会被查询出来

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

3.举例:

-- 按照性别分组,分别查询男、女同学的平均分

SELECT sex ,AVG(math) FROM db1 GROUP BY sex;

-- 按照性别分组,分别查询男、女同学的平均分,人数】

SELECT sex ,AVG(math),COUNT(id) FROM db1 GROUP BY sex;

-- 按照性别分组,分别查询男、女同学的平均分,人数,要求:分数低于

       70分的人,不参与分组

       SELECT sex ,AVG(math),COUNT(id) FROM db1 WHERE math>70        

       GROUP BY sex;

       -- 按照性别分组,分别查询男、女同学的平均数,人数,要求:分数低于

       70分的人,不参与分组,分组之后人数要大于2

       SELECT sex ,AVG(math),COUNT(id) FROM db1 WHERE math>70

       GROUP BY sex HAVING COUNT(id)>2;

4.分页查询

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

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

-- 每页显示3条记录

select * from student limit 0,3;--第一页

select * from student limit 3,3;--第二页

select * from student limit 6,3;--第三页

3.分页操作是一个Mysql “方言

 

 

 

posted @ 2021-04-24 19:34  JK~  阅读(65)  评论(0编辑  收藏  举报