当前位置:网站首页>MySQL quicklearn-2021-09-01
MySQL quicklearn-2021-09-01
2022-07-26 10:44:00 【ZhuMou】
0. Abstract
a. Sort
b. Sort + Screening
c. Single line processing functions ( Data processing functions )
d. Multiline processing functions ( Union function ) And group query
e.distinct keyword
1. Sort , Sorting and screening
| order by fieldName asc; | according to fieldName The fields are arranged in ascending order . Sorting and of strings C,Java similar . |
| order by fieldName desc; | according to fieldName The fields are arranged in descending order |
| order by fieldName; | according to fieldName The fields are arranged in ascending order ( The default is ascending ) |
| order by field1 asc, field2 asc...; | First of all, in accordance with the field1 Ascending order of ( It can also be in descending order ) Arrange ; In case of equality , Again according to field2 Ascending order of ( It can also be in descending order ) Arrange ... In short, the priority written in front is high . |
| select field1, field2,... from tableName order by ... | Arrange the table in the specified order of the specified fields |
example : surface stuinfor as follows
+------+-----+--------+
| name | age | sex |
+------+-----+--------+
| Guo | 19 | female |
| Wen | 25 | female |
| Xu | 21 | male |
| Ying | 21 | female |
+------+-----+--------+
First according to age Sort , Again according to name Conduct .
SELECT * FROM stuinfor ORDER BY age desc, name\g
Screening + Sort
| SELECT filed1,field2,... FROM tableName WHERE expr ORDER BY... | Filter and sort , The order of keywords cannot be changed . |
The execution order of the above statements is :FROM( Find table )--WHERE( Filter by criteria )--SELECT( lookup )--ORDER BY( Sort ). The final output . Note that the order of keywords cannot be changed .
For example, rank the women in the above table :
SELECT * FROM stuinfor WHERE sex = 'female' ORDER BY age desc\g
2. Single line processing functions
Single line processing functions , Also known as data processing function , Different from multi line processing function . Its characteristics are : An input , An output ; Several inputs , Several outputs . Although the teacher didn't emphasize , But a single line handler should have formal parameters , Arguments and return values .
| The function prototype | function | other |
| String lower(String); | Convert a string from uppercase to lowercase | Encountered non letters remain unchanged |
| String upper(String); | Convert a string from lowercase to uppercase | Encountered non letters remain unchanged |
| String substr(String,left,length); | Take the substring of the given character , And back to | about mysql, The subscript of the string is from 1 Start instead of 0. |
| int length(String); | Returns the length of the specified string | The difference in java Medium object.length. |
| String trim(String); | Remove spaces from the given string , And back to | It is often used for preliminary processing of input data . Remove the space before and after . The middle space cannot be removed . |
double rand(); double rand(int); | Generate [0,1) | Generate non repeatable random numbers in the default mode ,rand(const_n) Generate repeatable random numbers . |
| double round(double,int); | Round to the specified number of digits | Rounds the given floating-point number to the specified number of digits ,0 For a bit ,1 Represents ten ,2 Represents the percentile ......-1 On behalf of ten ,-2 Represent hundreds . |
| double ifnull(data,double) | Check whether the specified data is bit NULL. if NULL Returns the specified value , Otherwise return the original value . | because mysql in NULL Participate in arithmetic operations , The result must be NULL. To avoid this phenomenon , Therefore adopt ifnull function . |
case...when...then... when...then...else...end | take case The following variables are related to when After the match , And the corresponding then The value of the expression after is taken as the value of the whole function ; otherwise , take else The value after is taken as the value of the whole function . As long as a when If the condition is met, the following statement will be skipped , Direct to end. | about else Is it a necessary question : Not required . when Followed by the value , Instead of logical expressions ;then It should be followed by value ;case Followed by variables or fields . namely : case fieldName when fieldValue then expr1 else expr2; |
Single line handlers can form expressions , Can be used wherever expressions can be used . such as select after ,where Then, together with logical operators, they form logical expressions, and so on .
3. Multiline processing functions
The last part of single table query . Multi line processing functions are different from single line processing functions , Multiple input, One output. There are a total of 5 individual , as follows :
| int count(fieldName); | Return the specified field that is not NULL The number of elements |
| double sum(fieldName); | Return the specified field that is not NULL The sum of the elements of |
| double avg(fieldName); | Returns the average value of the elements in the specified field , Don't consider NULL Elements |
| double max(fieldName); | Returns the value of the largest element in the specified field |
| double min(fieldName); | Returns the value of the smallest element in the specified field |
Several considerations for using grouping functions :
1. When using the grouping function, it does not consider that the field is NULL The elements of , So you don't need to use it ifnull Pre treatment ;
2. Grouping function must be grouped before use . Grouping needs group by keyword , If it is not used, the whole table is a group by default .
3.where The grouping function cannot be used later , This is related to the execution order of various statements . It will be elaborated later .
There are two new keywords in the group query :
| group by fieldName1, fieldName2, ......; | Group according to the specified field . The number of groups should be the product of the number of groups of each field . As before select, You can only take group by Fields after . |
| having | Grammatically similar to where, For conditional screening , The difference lies in the execution order of statements . |
4. Group query
Consider the execution order of the following statements :
select ......
from ......
where ......
group by ......
having ......
order by ......\g
The order of arrangement cannot be changed , The order of execution is :1. Use from Find table ;2. Use where To filter ( Filtering immediately after finding the table is conducive to improving the execution efficiency );3. Use group by Grouping ( because group by The order of execution is where after , therefore where Grouping functions cannot be used after , Because it violates “ Group first and then use ” Principles );4. Use having Further filtering after grouping ( Able to use where Instead, try to use where Instead of , To improve the efficiency of execution );5. Use select Take the specified data from the table ( Be careful , With order by after select You can only take group by Fields after );6. utilize order by Sort . The final output .
There's a question to think about :order by Yes, it will sort within the Group , Or overall ranking ? The whole problem is not true in single table query . Because after grouping, you can only see the statistical information in the Group , Can't see the specific details , Not to mention sorting within groups .
5.distinct keyword
usage :select distinct fieldName1, fieldName2... from tableName;
distinct Must be placed in all field names ( Variable ) front , Used to remove duplicate records . If there are multiple field names after it , It means that multiple fields jointly remove duplicate records . When multiple fields jointly remove duplicate records , Only all fields of a certain two records ( stay distinct After that ) The values of are all the same before they are considered to be repeated ; As long as there is one field different , Will keep both .
have access to count and distinct Calculate the number of records that are not repeated .
select count(distinct fieldName1,...) from table;
边栏推荐
猜你喜欢

第6期:大学生应该选择哪种主流编程语言
![[leetcode daily question 2021/5/8]1723. The shortest time to complete all work](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work

从蚂蚁的觅食过程看团队研发(转载)

Anaconda is used on vscode (the environment has been configured)

Add touch screen driver for stemwin 5.22 on Shenzhou IV development board

The problem of large fluctuation of hx711 data

Disable usbjatg in Altium Designer

RT thread learning notes (V) -- edit, download and debug programs

SAP ABAP 守护进程的实现方式

Issue 5: the second essential skill for College Students
随机推荐
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming
如何实现临时的图形要素现实
Flutter 防止科学计数并去除尾数无效0
display-inline+calc实现左中右布局,中间自适应
面试知识点
PLC与伺服电机连接
2021-08-14三子棋
Sql Server 数据库之初学体验
$router和$route的区别
剑指Offer(五十二):正则化表达式
用两个栈实现队列
(转载)ArcGIS Engine中各种点的创建方法
Common classes (understand)
JS对象赋值问题
剑指Offer(五十三):表示数值的字符串
Flutter TextField怎样去除下划线及有焦点时颜色
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
字典与int矩阵
12 don't forget every component when copying an object