数据查询语言DQL(一)

目录

数据查询语言的基本概念:

条件查询

范围查询

 模糊匹配:

分页查询

查询结果排序

分组查询


数据查询语言的基本概念:

        DQL(数据查询语言)用于对数据库关系表中的数据进行查询(读取),其支持多种查询方式

扫描二维码关注公众号,回复: 14837819 查看本文章

        数据查询语言关键字:select

        查询语句基本格式:select <属性名> from <[数据库名.]表名>;

1、查询所有字段

select * from <表名>;

#查询所有字段用*星号

2、查询指定字段

select <属性名1[,属性名2,....,属性名n]> from <表名>;

#指定属性名查询数据

# 查询语句的基本格式select
-- 查询所有学生的所有信息
select * from db_2.student;
-- 查询所有学生的指定数据
select s_id,s_name from student;

条件查询

        条件查询基本格式:

        select <属性名> from <[数据库名.]表名> where 查询条件;

        条件查询的基本运算符:

        1、条件运算符:>、<、、=、!=(<>)、>=、<=(不存在==)

        2、逻辑运算符:&&(and\AND)、||(or\OR),用于连接两个条件

#关系运算符(>,<,>=,<=,=,!=(<>) )
select * from db_2.student where s_id >= 10086;
-- 在where子句之后加上查询条件
select * from db_2.student where s_id <> 10007;
-- <>是不等于符号(相当于!=)

#逻辑运算符(&&(and/AND),||(or/OR) )
select * from db_2.student where s_id >= 1006 && s_id <= 1012;

范围查询

       1、 在.....范围内查找:

        between......and......

        在....范围外查找:

        not between.....and......

       2、在集合范围内查找

        in(......)

        在集合范围外查找:

        not in(.....)

        #由于in 的内部处理没有where好,一般很少用in进行条件查询

#范围查询(连续范围内:数字、字母)
-- between ...and...; 在....之间
select * from db_2.student where s_id between 1006 and 1012;
-- not between...and...: 不在...之间
select * from db_2.student where s_id not between 1006 and 1012;

#集合范围查询(使用不连续范围,自定义范围)
-- in: 在集合范围内查询
select * from db_2.student where s_id in(1000,1011,1003,1000,'abc')
-- not in :在集合范围外查询
selectselect * from db_2.student where s_name not in(1000,"陈",1003)

 模糊匹配:

字符串的模糊匹配

字符串模糊匹配关键字:like

可用于根据关键字搜索。

模糊匹配的通配符:

 通配符%:匹配任意0个、一个或多个字符、

通配符_:匹配任意一个字符

# 字符串模糊匹配like (只适用于字符串等文本类型)
 -- like: 像....一样,类似于....
 -- 通配符'%':匹配任意多个字符
	-- 查询姓名以"小"字开头以任意多个字符结尾的学生信息
select * from student where s_name like "小%";
-- 通配符'_':匹配任意一个字符
	-- 查询姓名以“小”字开头以任意一个字符结尾的学生信息
select * from student where s_name like "小_";
-- 通配符可以配合或者组合使用
	-- 查询姓名以"小"字开头以任意两个字符结尾的学生信息
select * from student where s_name like "小_";
	-- 查询姓名以"小"字开头且在至少有两个字符的学生信息
select * from student where s_name like "小_%";

分页查询

基本格式: limit [偏移量n, ] 记录条数m

分页查询可以有两个参数,也可以只有一个参数,第一个参数为偏移量n,第二个参数为记录条数m,意思是查询从第n+1条记录开始的后m条数据,可以不给定偏移量,也就是只有一个参数时偏移量n默认为0。

# 分页查询limit
 -- 查询学生表中的前8 (o~7)条数据
 select * from student limit 8; -- 起始值默认为0
 -- 相当于:select * from student limit 0,8;
 -- 查询学生表中从第3(2+1)条数据开始后6条数据
 select * from student limit 2,6;
 

查询结果排序

对查询结果进行排序

        有时候我们需要对查询结果进行升序或降序排列,这就需要用到查询结果的排序了。

        排序关键字:order by (默认升序),

        一般格式:select <属性名> from <表名> order by <属性>;

        如:

        select s_id,s_name from students order by s_id;

        #order by 属性名 desc;(desc 降序排列)

        select s_id,s_name from students order by s_id desc;

# 查询结果order by
-- order by :查询排序(默认升序排列)
	-- 查询学生信息按年龄从小到大排序
select * from student order by s_age;

-- order by 属性名 desc:降序排列)
	-- 查询学生信息按年龄从小到大排序
select * from student order by s_age desc;

        

分组查询

        对查询结果进行分组。

        分组关键字:group by

        一般格式:

                select 属性名1,属性名2,.....,属性名n

                        from 表名 group by 属性名 (1~n) [having 条件表达式] ;

        #注意:分组过后不能用where进行条件筛选,需要用having

        where:从数据源去掉不符合搜索条件的数据

        having:在分组中去掉每组不符合条件的数据

#分组查询 group by
-- 对学生性别进行分组查询
select * from student group by s_sex;
-- 对学生年龄进行分组查询
select * from student group by s_age;
-- 单纯的分组查询意义不大(可以理解成为某个数学去除重复数据)
-- 分组查询一般用于分组之后的处理或者计算(分组求和,最大值,最小值,平均值,统计等)
	-- 分组统计
	-- 分别统计学生中男女生人数
select s_sex,count(s_id) from student group by s_sex;
-- count为统计函数

-- 分别统计班级学生人数
select s_cid,count(s_id) from student group by s_cid;
-- 分组之后的条件筛选
	--分别统计班级学生人数
    -- select s_cid,count(s_id) from student group by s_cid where s_cid is not null;
    -- is not null 为查询非空值,is null 为查询空值
    
    -- 分组之后无法使用where进行条件筛选,只能用having
    -- 分别统计班级学生人数
    -- select s_cid,count(s_id) from student group by s_cid having  s_cid is not null;
    
-- having 的查询效率比where低
select s_cid,count(s_id) from student where s_cid is not null group by s_cid;
-- 能在分组之前进行条件筛选的,尽量在分组之前用where筛选完成之后再分组

猜你喜欢

转载自blog.csdn.net/m0_65334415/article/details/128536324