[mysql review]——single table query knowledge review

I like a sentence very much: There is only one true heroism in this world, that is, you recognize the truth of life but still love it;

Table of contents

Conditional query (query data that meets conditions)

important point:

        When learning keywords such as mathematical formulas and sorting, you must first understand where they are used in the statement, format requirements, etc.;

Data sorting (order by field)

Keyword writing order

select ...

from ...

where ...

group by ... 

order by ...

Execution order

from ...

where ...

group by ...

having ...

select ...

order by ...

Common functions

Common single row processing functions

grouping function

important point:

Group query (can be grouped)

Key conclusions:

In a select statement, if there is a group by statement, the select can only be followed by: the fields participating in the grouping, and the grouping function, and nothing else;

You can also combine two fields into one field (joint grouping of two fields)

important point

having can filter the data after grouping, but it must be used in conjunction with group by (but it is inefficient when used alone);

Single table query summary



Conditional query (query data that meets conditions)

Common conditional queries (add where + condition after the query statement):

  1. = (equal to), <> or != (not equal to), greater than or equal to, etc.

  2. between .... and .... (between two values)

    • Note: The principle of small left and large right must be followed and the interval must be closed.

  3. is null、is not null

    • Null in the database cannot be measured using the equal sign. Its actual meaning is that there is no value or nothing, so there cannot be the syntax (=null).

       where  salary is null;
  4. and (and), or (or)

     where job = "MANAGER" and salary > 3000;
     where job = "MANAGER" or salary > 3000;
    • and and or appear at the same time, and has higher priority, and parentheses can be used when they appear at the same time;

     //找出工资大于2500并且编号是10或20的;
     where sal > 2500 and deptno = 10 or deptno =20;//这么写容易出现歧义
     where sal > 2500 and (deptno = 10 or deptno =20);
  5. in (include, equivalent to an or), not in

     //找出工资大于2500并且编号是10或20的;
     where sal > 2500 and deptno in (10,20)

    Note: in here is not an interval, but a set;

  6. like (fuzzy query), % or underscore matching;

    Note: % matches any character; underscore can only match one character;

     //名字中含有A
     where dname like '%A%';
     //第二个字母是A
     where dname like '_A%';
     //找出名字中带有下划线的
     where name like '%_%';//这样就会查出所有
     where name like '%\_%'//转义字符

important point:

        When learning keywords such as mathematical formulas and sorting, you must first understand where they are used in the statement, format requirements, etc.;

Data sorting (order by field)

Common sorting methods:

  1. asc ascending order

  2. descdescending

 order by salary asc, ename asc;
 //salary在前,起主导作用,只有salary相同时,才会使用ename升序排列;

Keyword writing order

The order in which keywords are used cannot be changed;

select ...
from ...
where ...
group by ... 
order by ...

Execution order

from ...
where ...
group by ...
having ...
select ...
order by ...

Common functions

Common functions are divided into single-row processing functions (processing several rows and outputting several rows), and multi-row processing functions (processing multiple rows and returning one row of results).

Common single row processing functions
  1. lower() converts to lowercase

    select lower(uname) as ename from emp;
  2. upper() converts to uppercase

  3. substr() takes a substring, format: substr (intercepted string, starting subscript, intercepted length)

    Note: The starting index starts from 1

     where substr(ename,1,1) = 'A';
  4. length() takes the length

  5. trim() to remove spaces

  6. str_to_date() Convert string to date

  7. date_format() format date

  8. round() rounds

  9. rand() generates random numbers

  10. ifnull() can convert null into a specific value


grouping function

Grouping function is also called multi-row processing function

  1. count count, sum summation, avg average value, max maximum value, min minimum value

  2. Note: The grouping function must be grouped first before it can be used. That is to say, according to the order of execution, the grouping function can be used after group by.

        Error demonstration:

    select ename,sal from emp where sal > min(sal);

    This is no problem in theory, but in practice an error will be reported;

     Correct example:

    select min(sal) from emp;
  3. If there is no grouping, then by default a table is a group

    select max(salary) from emp;

important point:

  1. If null is encountered during operations in the database, the result will be null, but null will be automatically excluded in the grouping function;

  2. null is not a value, it means nothing;

  3. The result obtained by count (a certain field) is the number of fields in the data table that are not null;

Group query (can be grouped)

In actual needs, we will encounter the need to group the data table first, and then query after grouping;

  Case demonstration:

 select ename,job ,sum(sal) from emp group by job;

The above statement can only be run in MySQL. It is meaningless in itself, but an error will be reported in Oracle. This should be because Oracle's syntax requirements are stricter than mysql.

Key conclusions:
In a select statement, if there is a group by statement, the select can only be followed by: the fields participating in the grouping, and the grouping function, and nothing else;
You can also combine two fields into one field (joint grouping of two fields)
 select deptno,job,max(sal) from emp group by deptno,job;

important point

  1. After adding group by, the fields after select cannot be written at will. You can only write the fields and grouping functions after group by;

  2. The grouping function cannot be written after where. This is related to the execution order. Group first and then use the grouping function;

  3. Why can the grouping function be used after select? It is also related to the execution order. Select is executed after group by;

 //找出每个部门中薪资最高的,显示的最高薪资要大于3000
 select ename,job,max(sal) from emp where sal>3000  group by ename,job;
having can filter the data after grouping, but it must be used in conjunction with group by (but it is inefficient when used alone);

Inefficient code demonstration:

 select ename,job,max(sal) from emp   group by ename,job having sal>3000;

In order to solve the above low efficiency problem, we can use where and having together. At this time, conditional deletion will give priority to where. If where cannot be completed, having will be considered;

Optimized strategy:

 select ename,job,max(sal) from emp where sal>3000  group by ename,job having sal>30;
//这个having加的没有意义,我只想在此处强调where和having可以联合使用以及优先级;

Such optimization seems stupid, but in fact, in some practical applications, there are problems that cannot be solved by where and need to be solved by having. For example: find out the average salary of each department and require the displayed average salary to be higher than 2,500. To address this need, we need to having solved

Single table query summary

  1. You can first check the data from the table, filter it by where, and then filter the filtered results by having;

  2. For the grouping function, its grammatical position is after select;

  3. After using group by to perform group query, pay special attention to the fields after select;

  4. Keyword execution order, from -- where -- group by -- having -- select -- order by;

Guess you like

Origin blog.csdn.net/m0_64231944/article/details/132069240