Home >Database >Mysql Tutorial >Advanced summary of MySQL single table query
This article brings you relevant knowledge about mysql, which mainly introduces the relevant content about single table query. Let’s take a look at it together. I hope it will be helpful to everyone.
Recommended learning: mysql video tutorial
When we operate on data, query is undoubtedly crucial, query The operation is flexible and changeable. We can design efficient query operations according to development needs and display the data stored in the database to users.
Query is a crucial part of data operations.
For example, if you want to find all products with prices within a specified range among all products, you want to put the data in the database on the client When displayed to users, query operations are generally performed.
In actual development, we have to decide how to query based on different needs and consider the efficiency of the query. Before learning the query, you can take a look at the complete syntax of the query:
SELECT 字段列表FROM 表名列表WHERE 条件列表GROUP BY 分组字段HAVING 分组后条件ORDER BY 排序字段LIMIT 分页限定
Based on For the keywords in the complete syntax of the query, we will learn basic query, conditional query, sorting query, grouping query and paging query respectively.
We use the following case to learn 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:
Query multiple fields:
select 字段列表 from 表名;
Query all fields:
select * from 表名;
Removing duplicate records:
select distinct 字段列表 from 表名;
Aliasing operation:
select 字段名 别名 from 表名;
We use the case in the preface to perform basic query Exercise:
Exercise of querying multiple fields:
select name,math from stu;
Exercise of alias operation:
select name,english 英语成绩 from stu;
General syntax:
select 字段列表 from 表名 where 条件列表;
Conditional query generally matches the operator To proceed, the following are several common operators:
Operator | Function Description |
---|---|
> | Greater than less than equal to not equal to |
between…and… | Within this range |
in(…) | Choose one more |
is null / is not null | is null / is not null |
and or&& | and |
or or|| | or |
select * from stu where age>20;
Query information about students whose age is equal to 18 years old, or whose age is equal to 20 years old, or whose age is equal to 21 years old:
select * from stu where age in(18,20,21);
Fuzzy query uses the like keyword, and wildcards can be used for placeholders:
select * from stu where name like '%张%';
select 字段列表 from 表名 order by 排序字段名1 [排序方式]...;
Note: There are two sorting methods: ascending ASC and descending DESC. The default is ascending ASC.
Function | |
---|---|
Statistical quantity (generally choose a column that is not null) | |
Maximum value | |
Minimum value | |
Sum | |
average |
The above is the detailed content of Advanced summary of MySQL single table query. For more information, please follow other related articles on the PHP Chinese website!