MySql教程

DQL mysql

本文主要是介绍DQL mysql,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

## DQL:查询表中的记录

        * select * from 表名;

        1.  语法:

                select

                         字段列表

                 from

                         表名列表

                 where

                          条件列表

                group by

                          分组字段

                having

                        分组之后的条件

                order  by

                         排序

                limit

                         分页限定

         2.  基础查询:

                1.  多个字段的查询

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

                           select name,age  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 name,math,english,math+if(english,0)from student;

                 4. 起别名:

                           *as :as也可以省略

                                             select name,math  (as)数学,english 英语,math+if(english,

                                              0) as 总分 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=22,or age=18 or age=25

                           select * from student where age in(22,18,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 “方言”

 

 

 

这篇关于DQL mysql的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!