[Mysql] Basics: DQL (data query language): Query the records in the table in the database - select

insert image description here

  • Profile of the blogger: A hard-working freshman majoring in computer science, who loves learning and creation. Currently learning and sharing: data structure, Go, Java and other related knowledge.
  • Blogger's homepage: @is Yaoyaozi
  • Belonging to the column: Mysql from entry to proficiency
  • Short-term goal: write every article in the column well

insert image description here

一、DQL(data query language)

Data query language, used to query records in database tables——select

In actual business logic, query operations are much larger than add, delete, and modify operations.

Imagine the following, when I open the shopping software, enter keywords, and search, in fact, it is the operation of querying data in the database, and then returning the data to the page; when we select the price range, it is a conditional query; at the same time, we can query The results are sorted according to sales, ratings, etc... These are all database query operations

Two, DQL statement: grammatical structure

  • select 字段列表: Select which fields to query
  • from 表名列表: Select which table to query in (currently based on single-table query)
  • where 条件列表: Add query conditions (based on what conditions to query, price range...)
  • group by 分组字段列表:
  • having 分组后条件列表
  • order by 排序字段列表
  • limit 分页参数

3. Query operation

3.1: Basic query

  • 1. Query and return multiple field data in the specified table
select 字段1,字段2,字段3... form 表名;

The above is to query the specified fields in the specified table, you can use wildcards *to query all the fields in the specified table

slect * from 表名;

Note: However, in the actual project, this method of querying all fields does not use wildcards; there are two reasons: 1, it is not intuitive (not very readable); 2, the execution efficiency is low

  • 2. Set an alias for the query field to enhance readability
select 字段1 [as 别名1],字段2 [as 别名2]... from 表名;
  • 3. Remove duplicate records
select distinct 字段列表 from table;

3.2: conditional query (where)

  • Syntax (query the specified field in the specified table according to wherethe restrictions of the following conditions)
select 字段列表 from 表名 where 条件列表;
  • About wherethe post-conditions
    insert image description here

Notice:

  • Range query between ... and ...: betweenfollowed by minimum value, andfollowed by maximum value
  • Logical operators are more commonly used andthan&&

Example:

select * from emp where age = 88;
select * from emp where age <= 20;
select * from emp where idcard is null;
select * from emp where idcard is not null;
select * from emp where age != 88;
select * from emp where age <> 88;
select * from emp where age >=15 and age <=20;
select * from emp where age between 15 and 20;
select * from emp where gender = '女' and age < 25;
select * from emp where age = 18 or age = 20 or 40;
select * from emp where age in(18,20,40);
select * from emp where name like '__'; #查询名字为两个字的员工信息(使用两个占位符)
select * from emp where idcard like '%x'; #查询身份证号最后一位是`x`的员工信息

3.3: aggregate function (count max, min)

3.3.1: What is an aggregate function

Aggregation functions can be used to perform longitudinal calculations on a column of data as a whole. They are all acting on a certain column in the table !

3.3.2: Common aggregate functions

aggregate function Function
count Count the number of records in a column or the entire table
max Find the maximum value of a column
min Find the minimum value of a column
avg Find the average value of a column
sum Sum (a column)

3.3.3: How to use aggregate functions

  • Use the aggregation function on the specified field data in the specified table
select 聚合函数(字段列表) from 表名;

Note: null value data does not participate in aggregation function operations

Example:

#统计员工数量
select count(*) from emp;
 #统计企业员工的平均年龄
 select avg(age) from emp;
 #求所有员工中最大年龄
 select max(age) from emp;
 #求最大年龄
 select min(age) from emp;
 #查询所有在西安工作的员工的年龄之和
 select sun(age) from emp where workaddressed = '西安';

3.4: Group query (group by)

  • whereSyntax: Query the specified field in the specified table according to the condition , and group according to the grouping field (you can add the filter condition after grouping)
select 字段列表 from 表名 where 条件 group by 分组字段名 [having 分组后进行过滤的条件];
  • Notice
    • Execution order:where > 聚合函数 >having
    • The fields to be queried are generally: aggregate functions and grouping fields, and it is meaningless to query other fields
  • difference between where and having
    • Execution order: wherethe condition is to filter data before grouping, havingand to filter data for each group after grouping
    • wherehavingThe aggregation function cannot be judged,
      for example:
#根据性别进分组,统计男性员工和女性员工的数量
select gender,count(*) from emp group by gender;
#根据性别进行分组,统计男性员工和女性员工的平均年龄
select gender,avg(age) from emp group by gender;
 #查询年龄小于45的员工,并且根据工作地址进行分组,获取员工数量大于等于3的工作地址
 select workaddress,count(*) from emp where age < 45 group by workaddress having count(*) >= 3;

3.5: sort query (order by)

  • Syntax: Query the specified field in the specified table, and sort according to the sorting field and its sorting method
select 字段列表 from 表名 ordered by 字段1 排序方式1,字段2 排序方式2;
  • Sort by: divided into two
    • asc: Abbreviation for Ascending
    • desc: Abbreviation for Descending
  • Notice
    • The previous desc (describe) is to view the table structure, this desc is in descending order
    • The default is to ascsort in ascending order, so when the sorting method is ascending, you can omitasc
    • Multi-field sorting, with the first field as the first key and the second field as the second key

Example:

#根据年龄对公司员工进行升序排序,如果年龄相同,再安装入职时间进行降序排序
select * from emp order by age asc, entrydate desc;

3.6: Paging query (limit)

  • indexSyntax: The query starts from the specified index , including nthe records of the specified index
select 字段列表 from 表名 limit 起使索引index,查询记录数n;
  • Notice
    • How to remember it? Think of each piece of data in the table as an element in the array, starting from the index and the subscript of the array, starting from the beginning, then the 0pagination query means starting from the index index, including indexthe first and last nrecords/fields

    • 起使索引 = (查询页码-1)* 每页显示的记录数; Page numbers generally start from 1, so -1 is required

    • Paging query syntax, different databases have their own different implementations, for mysql database it islimit

    • If the query is the data of the first page, then the index can be omitted, and the query statement is abbreviated as:select 字段列表 from 表名 limit 查询记录数n;

Example:

# 查询第一页员工数据,每页展示10条记录
select * from emp limit 0,10;
select * from emp limit 10;

# 查询第二页数据,每页展示10条记录
select * from emp limit 10,10;

4. Case exercises

  • create form
-- 数据准备
create table emp
(
    id      int comment '编号',
    worknum varchar(10) comment '工号',
    name    varchar(10) comment '姓名',
    gender  char comment '性别',
    age     tinyint unsigned comment '年龄',
    idcard  char(18) comment '身份证号码',
    workaddress varchar(50) comment '工作地址',
    entrydate date comment '入职时间'
)comment '员工表';
  • increase data
-- 添加数据
insert into emp(id,worknum,name,gender,age,idcard,workaddress,entrydate)
values (1,'1','瑶瑶子','女',19,'123456789012345671','北京','2004-10-03'),
       (2,'2','李明','男',20,'123456789012345672','上海','2003-2-1'),
       (3,'3','李华','男',14,'123456789012345673','武汉','2005-4-2'),
       (4,'4','张小红','女',20,'123456789012345674','武汉','2005-4-2'),
       (5,'4','黄小丫','女',22,'123456789012345675','天津','2006-4-2'),
       (6,'6','张小红','女',23,'123456789012345676','武汉','2007-3-9'),
       (7,'7','李晓东','男',64,'123456789012345677','哈尔滨','1999-11-4'),
       (8,'8','张浩','男',35,'123456789012345678','大连','2001-5-4');

  • Operation exercise
#1、查询年龄为20、22、23岁女性员工信息
select * from emp where gender = '女' and age in(20,21,22,23);

insert image description here

#2、查询性别为男,并且年龄再20-40以内(含),且姓名为3个字以内的员工
select * from emp where gender = '男' and age between 20 and 40 and name like '___';

insert image description here

#3、统计员工表中,年龄小于60的男性和女性数量
select gender, count(*) from emp where age < 60 group by gender;

insert image description here

#4、查询所有年龄小于35岁的员工的姓名、年龄,并对查询结果安装年龄升序排序,入职时间为第二关键字降序排序
select name,age from emp where age <= 35 order by age asc , entrydate desc ;

insert image description here

#5、查询性别为难,且年龄为20-40岁(含)以内的5个员工信息,并对查询结果安装年龄升序,入职时间降序
 select * from emp where gender = '男' and age between 20 and 40 order by age asc ,entrydate desc limit 5;

insert image description here

Five, select statement execution sequence comparison:

insert image description here

Note: For the selct statement, the order of execution is FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT


insert image description here

Guess you like

Origin blog.csdn.net/Yaoyao2024/article/details/130443519