MySQL database-DQL language-2
1. Common functions
1.1 The concept of common functions
Encapsulate a set of logical statements in the method body and expose the method name
1.2 Classification of common functions
a. Character functions
b. Mathematical functions
c. Date function
d. Other functions
f. Process control function
2. One-line function-character function
2.1 length-Get the number of bytes of the parameter value
Case: For example, get the number of bytes of the input value
SELECT LENGTH('jion');
2.2 concat-concatenating strings
Case: splicing the first name and last name in the employee table into names
SELECT CONCAT(first_name,last_name) FROM employees ;
2.3 upper and lower-change the parentheses to uppercase or lowercase
Case: Change the brackets to uppercase
SELECT UPPER('student');
Case: Change the brackets to lowercase
SELECT LOWER('STUDENT');
2.4 substr-intercept character
Note: MySQL index starts from 1 instead of 0
Case: Intercept all characters from the specified index
SELECT SUBSTR('I really like learning and I really like learning databases', 5) AS loves learning; //As a result I like learning and I really like learning databases
Case: Intercept the characters of the specified character length from the specified index
SELECT SUBSTR('I really like learning and I like to learn databases very much', 5, 10) AS loves learning;//I like learning and very much
2.5 instr-returns the index of the first occurrence of the substring, or 0 if not found
SELECT INSTR('I really like learning','learning'); love learning
2.6 trim removes the specified characters before and after the substring
SELECT TRIM ('AA' FROM 'AA 张 AA 三 AA');
2.7 lpad-Use the specified characters to achieve the length of the left padding
SELECT LPAD('Zhang Sansan',2,'*');
2.8 rpad- use the specified characters to achieve the length of the right padding
SELECT RPAD('Zhang Sansan',12,'ab');
2.9 replace
SELECT REPLACE('Zhang San','Three','Five');//Zhang Wu
3. One-line functions-mathematical functions
3.1 round-rounding
SELECT ROUND(1.55);//2, the same is true for negative numbers
3.1.1 round-Keep a few digits after the decimal point
SELECT ROUND(1.5555,3);//1.556, the same for negative numbers
3.2 ceil --Improvement arrangement
> Round up, return >= the smallest integer of the parameter
SELECT CEIL(1.00);//2
3.3 floor round down
SELECT FLOOR(-9.99);
3.4 truncate-Truncate
SELECT TRUNCATE(1.69999,1);//1.6
3.5 mod-take the remainder
SELECT MOD(10,3); // 1
4. Single line function-date function
4.1 now returns the current system date+time
SELECT NOW();
4.2 curdate returns the current system date, excluding time
SELECT CURDATE();
4.3 curtime returns the current system time; does not include the date
SELECT CURTIME ();
4.4 Get the specified part, year, month, day, hour, minute, second
#Get the current year of the system
SELECT YEAR(NOW());
#Get the current month of the system
SELECT MONTH(NOW());
#Get the current number of the system
SELECT DAY(NOW());
#Get the current hours of the system
SELECT HOUR(NOW());
#Get the system is currently a few minutes
SELECT MINUTE(NOW());
#Get the current number of seconds in the system
WHEN salary >20000 THEN 'A'
4.5 Date functions
STR_TO_DATE: Convert the characters in the date format to the date in the specified format
如: STR_TO_DATE('9-1-1999','%m-%d-%y')
DATE_FORMAT: Convert date to character
Such as: DATE_FORMAT('2018/6/6','%Y year%m month%d day')
5. Other functions
SELECT VERSION();//Query version number
SELECT DATABASE();//Query the current database
SELECT USER();//represents the current user
6. Flow control function
6.1 if function: the effect of if else
Such as: SELECT IF(10>5,'big','small');
6.2 case function: is a control structure
Use of case function: similar to the effect of switch case
In MySQL
case field or expression to be judged
when constant 1 then the value to be displayed 1 or statement 1;
when constant 2 then the value to be displayed 2 or statement 2;
....
else the value n or statement n to be displayed;
end
Case inquiries about the work situation of employees
If salary> 20000, display A level
If salary> 15000, display B level
If salary>10000, display C level
Otherwise, display D level
SELECT salary
CASE
WHEN salary >20000 THEN 'A'
WHEN salary >15000 THEN 'B'
WHEN salary >10000 THEN 'C'
ELSE 'D'
END AS salary grade