MySQL - single row processing function and multi row processing function

Data processing function:

Single row processing function: one input corresponds to one output

Multi-line processing function: multiple inputs correspond to one output

Single line processing function:

Convert uppercase to lowercase: lower(); select lower(ename) as ename from emp;

Convert lowercase to uppercase: upper(); select upper(ename) from emp;

Intercepted string: substr(name of the intercepted string, subscript of the intercepted position, intercepted length); note: the subscript of the intercepted position starts from 1, not 0!

select substr(ename,1,3) from emp;

We use substr to query names whose first letter is A: select ename from emp where substr(ename,1,1)='A';

Make the first letter of the employee's name lowercase: select concat(lower(substr(ename,1,1)),substr(ename,2,length(ename)-1)) as newname from emp;

Concatenate two strings: concat(string1, string2);

Calculate the length of the string: length();

Remove leading and trailing spaces: trim();

We can avoid this problem when we use trim:

Rounding: round (the data to be rounded, keep the number of decimal places)

Select can be followed by root field name (equivalent to variable name) or literal value/literal value (data)

Generate random numbers: rand();

select rand() from emp;

rand() is used with round():

select round(rand()*100,0) from emp;

You can convert null to the desired number: ifnull (field, if the field is null, you need to change it to the number)

Note: In database operations, when any number is used for mathematical operations on null, the result is null

When summing the data of these two fields, when a certain corresponding data is null, you don’t want it to be null, so you can change it into the desired number! ! !

case ......when......then........when.........then.......else.......end

When what are you, if..... then.... if..... then...otherwise..... ..Finish

select ename,job,sal,(case job when 'MANAGER' then sal*1.1 when 'SALESMAN' then sal*1.5 else sal end) as result from emp;

 


 

Grouping functions (multi-row processing functions):

Grouping function: it must be grouped first before querying. Group by is used for grouping. When there is no group by, the entire table is a group by default

Grouping functions cannot be used without grouping! ! !

Overview:

Count: count()

Summing: sum()

Average: avg()

Maximum value: max()

Minimum value: min()

Notice:

1. The grouping function automatically ignores NULL, and automatically skips when there is NULL in the data.

Originally there were 14 data in total, but in the end there were only 4!

2. The difference between count(*) and count (specific fields):

'*': Represents the use of all fields in a row

Because there is no group by, a table has only one group, so as long as one of the data in a row is not NULL, then the data in this row is valid.

3. The grouping function cannot be used in where, because in select.....from.....where...group by....having....order by .....the first is from, then where, then group by, then having, then select and finally order by.

Because the grouping function can only be used after grouping, where is before group by, and group by has not been grouped at that time, so it cannot be used in where!

4. All grouping functions can be used at the same time:

Group query:

If you group each department first, and then find the average salary of each department:

Then can we add an ename? We know that this is meaningless. The average salary of each department calculated by this is obviously meaningless with ename.

It can still run in the mysql57 version, but it will report an error in the mysql80 and oracle databases! ! !

Therefore, if the query statement contains group by, then select can only follow the fields participating in the grouping, grouping functions, and other fields cannot! ! !

In select deptno,avg(sal) from emp group by deptno; where deptno is the field involved in the grouping, avg(sal) is the grouping function.

Group by fields of different multiples:

Query the highest salary in different departments and positions:

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

Having must be used in conjunction with group by, and it is a supplement to group by, because sometimes where cannot be grouped, so it cannot be solved. At this time, the role of having comes!

If both where and having can be solved at the same time, use where first, which is more efficient!

For example: Find out the average salary of each department, and then ask to display the average salary higher than 2500!

The average salary needs to use the grouping function at this time, and there is still a choice to judge that it is greater than 2500. At this time, where will not work, and using having is just right!

distinct keyword

distinct: the original table records will not be modified, but will only be deduplicated.

Note: distinct can only be written in front of all fields, not just in front of a certain field!

For example: when there are two fields, it is to combine the two fields for deduplication!

At the same time: distinct can also be used in combination with grouping functions: select count(distinct job,deptno) from emp;

 

Guess you like

Origin blog.csdn.net/m0_73968621/article/details/132570526