When we operate on data, query is undoubtedly crucial. The query operation is flexible and changeable. We can design efficient query operations according to the needs of development, and display the data stored in the database to users.
Article directory
foreword
查询是数据操作至关重要的一部分,
For example, if you want to find out all the products whose prices are within the specified range among all the products, if you want to display the data in the database to the user on the client, you usually have to perform a query operation.
In actual development, we need to decide how to query according to different needs and consider the efficiency of the query. Before learning query, you can first look at the complete syntax of the query:
SELECT
字段列表
FROM
表名列表
WHERE
条件列表
GROUP BY
分组字段
HAVING
分组后条件
ORDER BY
排序字段
LIMIT
分页限定
According to the keywords in the complete syntax of the query, we learn separately基础查询,条件查询,排序查询,分组查询和分页查询。
We use the following case study single table query:
-- 删除stu表
drop table if exists stu;
-- 创建stu表
CREATE TABLE stu (
id int, -- 编号
name varchar(10), -- 姓名
age int, -- 年龄
gender varchar(5), -- 性别
math double(5,2), -- 数学成绩
english double(5,2) -- 英语成绩
);
-- 添加数据
INSERT INTO stu(id,name,age,gender,math,english)
VALUES
(1,'小张',23,'男',66,78),
(2,'小李',20,'女',98,87),
(3,'小陈',55,'男',56,77),
(4,'小樊',20,'女',76,65),
(5,'小马',20,'男',86,NULL),
(6,'小赵',57,'男',99,99);
Select SQL execution in Navicat:
1. Basic query
1.1 Basic query syntax
Query multiple fields:
select 字段列表 from 表名;
Query all fields:
select * from 表名;
Remove duplicate records:
select distinct 字段列表 from 表名;
Alias operation:
select 字段名 别名 from 表名;
1.2 Basic query exercises
We use the cases in the preface for basic query exercises:
Exercises for querying multiple fields:
select name,math from stu;
Alias operation practice:
select name,english 英语成绩 from stu;
2. Conditional query
2.1 Conditional query syntax
General syntax:
select 字段列表 from 表名 where 条件列表;
Conditional queries are generally performed with operators, and the following are some common operators:
operator | Functional description |
---|---|
> < = ! | greater than less than equal to not equal to |
between…and… | within this range |
in(…) | choose one |
is null / is not null | is null / is not null |
and 或 && | and |
or 或 || | or |
2.2 Condition query practice
We use the case in the preface for conditional query exercises:
Query the information of students whose age is older than 20:
select * from stu where age>20;
Query the information of students whose age is equal to 18, or whose age is equal to 20, or whose age is equal to 21:
select * from stu where age in(18,20,21);
Fuzzy queries use the like keyword, and wildcards can be used for placeholders:
- _ : represents a single arbitrary character
- % : represents any number of characters
Query information about students whose names contain Zhang:
select * from stu where name like '%张%';
3. Sort query
3.1 Sorting query syntax
select 字段列表 from 表名 order by 排序字段名1 [排序方式]...;
Note: There are two sorting methods: ascending ASC and descending DESC, and the default is ascending ASC.
3.2 Sorting query practice
We use the case in the preface for sorting query practice:
4. Aggregate functions
4.1 Aggregate Function Syntax
What is an aggregate function? When performing query operations, we often need to perform operations on an entire column. For example, we can calculate the average value of an entire column of grade data, and we need to use aggregate functions. The following are common aggregate functions:
Function name | Function |
---|---|
count(column name) | Statistical quantity (generally choose a column that is not null) |
max(column name) | maximum value |
min(column name) | minimum value |
sum(column name) | to sum |
avg(column name) | average value |
General syntax:
select 聚合函数 from 表名;
Note: NULL values do not participate in aggregation function operations.
4.2 Aggregate function exercises
We use the case in the preface to practice the aggregate function:
Count how many students there are in this table:
select count(id) from stu;
Above we use a certain field to perform calculations. The problem we may face is that a certain value may be NULL, so we generally use *
to perform calculations, because it is impossible for all fields in a row to be NULL.
select count(*) from stu;
Query the average score of math scores:
select avg(math) from stu;
5. Group query
5.1 Group query syntax
select 字段列表 from 表名 [where 分组前的条件限定] group by 分组字段名 [having 分组后的条件过滤]
Note: After grouping, the fields to be queried are aggregation functions and grouping fields, and it is meaningless to query other fields.
5.2 Group query practice
We use the case in the preface for group query exercises:
Query the average math scores of male and female students, as well as their respective numbers. Requirements: those with scores below 70 do not participate in the group:
select gender, avg(math),count(*) from stu where math > 70 group by gender;
Query the average math scores of male and female students, as well as their respective numbers. Requirements: those whose scores are lower than 70 do not participate in the grouping, and those whose numbers are greater than 2 after grouping:
select gender, avg(math),count(*) from stu where math > 70 group by gender having count(*) > 2;
Note: where 和 having
The execution timing is different: where is limited before grouping, if the where condition is not met, it will not participate in grouping, and having is to filter the results after grouping. Therefore, where cannot judge the aggregate function, but having can.
6. Paging query
6.1 Paging query syntax
In everyone's impression, when a web page displays a large amount of data, it often does not display all the data at once, but also displays it in pages. In fact, it is the operation of querying the data in pages, that is, only querying the data display of one page at a time to the page.
select 字段列表 from 表名 limit 查询起始索引,查询条目数;
In limit
the keyword , the query starting index parameter starts from 0.
5.2 Paging query exercise
We use the case in the preface for pagination query practice:
Start query from 0, query 3 pieces of data:
select * from stu limit 0,3;
起始索引 = (当前页码 - 1) * 每页显示的条数
7. Summary
Combined with the practice of classic cases, we have completed all the basic and practice training of single table query. As the most important part of data operation, query must be practiced continuously to operate proficiently.