MySQL basics (2)-single table query theory of data query language DQL (1)

  We started from the last article, we have begun to introduce the basics of database. In the last article, we mainly introduced the basis of data, including the differences and connections of several large databases and the necessity of learning databases, as well as the relationship between databases and database management systems and data administrators. In addition, I introduced the data definition language, including the addition, deletion, modification, and query of the database, and the commands to enter the database. Finally, I introduced the data manipulation language, including adding, deleting, modifying and checking the data in the table. Finally, a comparison is made between delete and truncate. This article introduces to you the single-table query in database query, and deepens the understanding of each knowledge point through some cases. Next, I will introduce you to single-table query:

Full table query : select * from 表名;
Query specified columns : select 字段1[,字段2,…] from 表名;
Alias ​​settings : select 字段名 as 列别名 from 原表名 [as ]表别名;
Query unique records : select distinct 字段名 from 表名;
Condition query : select 字段1[,字段2,…] from 表名 where 查询条件;
Null value query : select 字段1[,字段2,…] from 表名 where 空值字段 is[ not] null;
Fuzzy query : select 字段1[,字段2,…] from 表名 where 字符串字段[ not] like 通配符;
  Percent sign (%) Wildcard: Match multiple characters
  underscore (_ ) Wildcard: matches one character
  Fuzzy query can only be used for string type fields

  Before introducing the code, we first enter the database

use test;
show tables;

  The execution results are as follows:

  The code for single-table query is implemented as follows:

select * from emp;

  The results of the specific implementation are as follows:

  but we need to pay attention to:The result of a single table query is just a virtual result set, That is, the result returned when we query the table is just a virtual temporary table, which will not occupy computer memory and will not affect our original data. Next, query the specified column, the specific code implementation is as follows:

select empname, job, sal from emp;

  The results of the specific implementation are as follows:

  set aliases for other fields, and the specific implementation is as follows:

-- 设置别名:查询每位员工调整后的薪资(基本工资+1000)
select *, sal+1000 as comm from emp;

  The results of the specific implementation are as follows:

  We strengthen our understanding of this knowledge point through an exercise:

  Query the annual salary of each employee (base salary*12): empno, ename, annual salary

select empno, empname, sal*12 as 年薪  from emp;

  The specific execution results are as follows:
  query unique data, the specific implementation is as follows:

select distinct deptno from emp;
select distinct deptno, job from emp;

  The results of the specific execution are as follows:

  Next, the conditional query is introduced, and the code is implemented as follows:

-- 查询基本工资大于等于2000小于等于3000的员工信息
select * 
from emp 
where sal >= 2000 and  sal <= 3000;

select * 
from emp 
where sal between 2000 and  3000;

  The results of the specific implementation are as follows:

  We use two cases to deepen our understanding of the knowledge point of conditional query

  Query the information of employees whose sal is lower than 2000 in Department 10 and Department 20

select * 
from emp
where (deptno = 10 or deptno = 20) and sal < 2000;

select * 
from emp
where deptno in (10,20) and sal < 2000;

  The results of the specific execution are as follows:

  What needs to be noted here is: in method 1, because of the sequence of operators involved, we need to add parentheses to change their priority. The reason why the sql command finds the empty is not because the command is written, but the data table itself is not lower than 2000. Therefore, the result is empty.

  Query the department of salesman: name, position, department

select empname, deptno,job
from emp
where job = 'salesman';

  The results of the specific execution are as follows:

  Next, we will introduce the null value query:

  Query records where mgr is empty

select * 
from emp 
where mgr is null;
-- 当然我们也可以查找不为空的记录
select * 
from emp 
where mgr is not null;

  The results of the specific execution are as follows:

  What we need to pay attention to here is: in the process of null value query, because the null value is a kind of character, not any kind of value, so you cannot use >, <, = to compare the size, but through is nullOr is not nullto express. We use a case to deepen our understanding of null query:

   Query records where comm is not empty

  The specific implementation is as follows:

select * 
from emp 
where comm is not null;

  The results of the specific implementation are as follows:

  Next, I will introduce the fuzzy query in the single-table query.

– Query the information of employees whose names begin with a

  The specific implementation is as follows:

select *
from emp
where empname like'a%';

  The results of the specific execution are as follows:

  Here we need to pay attention to: In the fuzzy query, the keyword we use is like. We use several cases to consolidate the fuzzy query knowledge points.

  Query the information of employees whose name contains a

  The specific implementation is as follows:

select *
from emp
where empname like'%a%';

  The specific implementation results are as follows:

  Query the information of the employee whose name is a as the second character

  The specific implementation is as follows:

select *
from emp
where empname like'_a%';

  The specific implementation results are as follows:

  Query employee information that does not contain s in the employee name

  The specific implementation is as follows:

select *
from emp
where empname not like '%s%';

  The results of the specific execution are as follows:

  We next introduce the sorting of the result query in the single table query:

   Query result sorting: select 字段1[,字段2,…] from 表名 order by 字段1[ 排序方向,字段2 排序方向,…];If sorting multiple fields, first sort by the first field, and then sort by the second field when the value of the first field is the same. Specify the sorting direction: asc ascending order, desc descending order (when the sorting direction is not specified, the default is asc ascending order)

   Single field sorting: query all employee information and display it in descending order of sal

  The specific implementation is as follows:

select *
from emp
order by sal desc;

select *
from emp
order by sal asc;

  The specific implementation results are as follows:

  Multi-field sorting: Query all employee information in ascending order of deptno and descending order of sal

  The specific implementation is as follows:

select *
from emp
order by deptno asc, sal desc;

  The results of the specific execution are as follows:

  If in sorting, if we do not specify whether the sorting of the field is descending or ascending, the default is ascending. Next, we will introduce the usage of limiting the number of query results in a single table query:

  Limit the number of query results: select 字段1[,字段2,…] from 表名 limit [偏移量,] 行数;Here are the things to note:limit accepts one or two numeric parameters, the parameter must be an integer constant, However, the first parameter specifies the offset of the first returned record row , and the second parameter specifies the maximum number of returned record rows . If only one parameter is given , it means to return the maximum number of record rows .The offset of the initial record line is 0 (instead of 1).
  Query the top 5 employees with the highest basic salary

  The specific implementation is as follows:

select *
from emp
order by sal desc
limit 5;

  The specific implementation results are as follows:

  Query the 6th to 10th employees of the basic salary

  The specific implementation is as follows:

select *
from emp
order by sal desc
limit 5,5;

  The specific implementation results are as follows:

  Query the last 5 employees who joined the company

  The specific implementation is as follows:

select *
from emp
order by hiredate
limit 5;

  Specific results of the implementation are as follows:

  Next, to introduce an aggregate function, we MySQL aggregate functions commonly used include avg(), , count(), max(), min(), sum()specified as follows:

  Query the total number of employees, maximum wage, minimum wage, average wage and sum of wages in the emp table

  The specific implementation is as follows:

select count(*) 员工总数, max(sal) 最高工资, min(sal) 最低工资,avg(sal) 平均工资, sum(sal) 工资总和
from emp;

  The results of the specific execution are as follows:

  Next, I will introduce the group query in the single table query:

  Group query : groupselect 字段1[,字段2,…] from 表名[ where 查询条件] group by 分组字段1[,分组字段2,…]; the query results according to one or more fields,The field values ​​are the same as a group, and each group is aggregated and calculated
 &emsp**;Filter after grouping**: select 字段1[,字段2,…] from 表名[ where 查询条件][ group by 分组字段1[,分组字段2,…]] having 筛选条件;
  Query the average salary of each department

  The specific implementation is as follows

select deptno, avg(sal) as 平均工资
from emp
group by deptno;

  The specific implementation results are as follows:

  Query the average salary of different positions in each department

  The specific implementation is as follows:

select deptno,job,avg(sal)
from emp
group by deptno, job;

  The specific implementation results are as follows:

  Query the number of employees in each department

  The specific implementation is as follows:

select deptno,count(*) 员工数
from emp
group by deptno;

  The specific implementation results are as follows:

  Query the number of people in different positions in each department

  The specific implementation is as follows:

select deptno,job,count(*) 人数
from emp
group by deptno, job;

  The specific implementation results are as follows:

  Query the average salary of clerk in each department

  The specific implementation is as follows:

select deptno, job, avg(sal)
from emp
group by deptno, job
having job = 'clerk';

select deptno, job, avg(sal)
from emp
where job = 'clerk'
group by deptno;

  The results of the specific execution are as follows:

  What we need to pay attention to here is: since having and where both represent filtering conditions, and sometimes they can be shared, the difference between the two is:

  • The where clause acts on the table , and the having clause acts on the group .
  • The scope of the where condition query is to filter the data table , while the having condition query is to filter the grouping results .
  • where the previous packet and the polymerization calculation filter rows, and having the following grouping and aggregate filter rows of the packet,The where clause cannot contain aggregate functions

  We introduce a case that can only be used havingbut not used where, as follows:

  Query the department where the average salary is greater than 2000

  The specific implementation is as follows:

select deptno, avg(sal) as 平均工资
from emp
group by deptno
having 平均工资 > 2000;

  The specific results of the execution are as follows:

  We have arithmetic operators, logical operators, comparison operators and the priority between them in MySQL single table query, as follows:

  Finally, I will introduce you to the general order of MySQL when writing query statements. :

  However, the order of execution of the MySQL database is:

  we started from the previous article, we have begun to introduce the basic knowledge of the database. In the last article, we mainly introduced the basis of data, including the differences and connections of several large databases and the necessity of learning databases, as well as the relationship between databases and database management systems and data administrators. In addition, I introduced the data definition language, including the addition, deletion, modification, and query of the database, and the commands to enter the database. Finally, I introduced the data manipulation language, including adding, deleting, modifying and checking the data in the table. Finally, a comparison is made between delete and truncate. This article introduces to you the single-table query in the database query, which mainly includes the entire table query, conditional query, aggregation function, group query, post-query filtering and sorting knowledge points, and each knowledge point is deepened by some cases. The reader's understanding of each point of knowledge. Finally, it introduces the writing order and execution order of MySQL in the query. Therefore, mysql is a very important skill. Almost every job in the computer needs a mysq skill. Therefore, we need special mastery. Life is endless and struggle is endless. We work hard every day, study hard, constantly improve our abilities, and believe that we will learn something. Come on! ! !

Guess you like

Origin blog.csdn.net/Oliverfly1/article/details/114776982