Data query language DQL (1)

Table of contents

The basic concepts of data query language:

conditional query

range query

 Fuzzy match:

Paging query

Query result sorting

Group query


The basic concepts of data query language:

        DQL (Data Query Language) is used to query (read) data in database relational tables, and it supports multiple query methods

        Data query language keyword : select

        The basic format of the query statement: select <property name> from <[database name.] table name>;

1. Query all fields

select * from <table name>;

#Query all fields with * asterisk

2. Query the specified field

select <attribute name 1[, attribute name 2,....,attribute name n]> from <table name>;

#Specify the attribute name to query data

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

conditional query

        Basic format of conditional query:

        select <attribute name> from <[database name.] table name> where query condition;

        Basic operators for conditional queries:

        1. Conditional operators: >, <, , =, !=(<>), >=, <= (== does not exist)

        2. Logical operators: &&(and\AND), ||(or\OR), used to connect two conditions

#关系运算符(>,<,>=,<=,=,!=(<>) )
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;

range query

       1. Search in the range of .....:

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

        Find outside of ....:

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

       2. Search in the collection range

        in(......)

        Look outside the bounds of a collection:

        not in(.....)

        #Because the internal processing of in is not as good as where, generally in is rarely used for conditional query

#范围查询(连续范围内:数字、字母)
-- 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)

 Fuzzy match:

Fuzzy matching of strings

String fuzzy matching keyword : like

Can be used to search by keyword.

Wildcards for fuzzy matching:

 Wildcard%: Match any 0, one or more characters,

Wildcard _: matches any character

# 字符串模糊匹配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 "小_%";

Paging query

Basic format : limit [offset n, ] number of records m

Pagination query can have two parameters, or only one parameter, the first parameter is the offset n, and the second parameter is the number of records m, which means to query the last m pieces of data starting from the n+1th record , the offset can not be given, that is, the offset n defaults to 0 when there is only one parameter.

# 分页查询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;
 

Query result sorting

Sort query results

        Sometimes we need to sort the query results in ascending or descending order, which requires sorting the query results.

        Sorting key : order by (default ascending order),

        General format: select <attribute name> from <table name> order by <attribute>;

        like:

        select s_id,s_name from students order by s_id;

       #order by attribute name desc; (desc descending order)

        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 query

        Group query results.

        Grouping keyword: group by

        General format:

                select attribute name 1, attribute name 2, ..., attribute name n

                        from table name group by attribute name (1~n) [having conditional expression];

        #Note: After grouping, you cannot use where to filter conditions, you need to use having

        where: Remove data that does not meet the search criteria from the data source

        having: Remove each group of unqualified data in the group

#分组查询 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筛选完成之后再分组

Guess you like

Origin blog.csdn.net/m0_65334415/article/details/128536324