Advanced # 4: Common Functions
/ * Concept: similar to Java methods, a set of logic statements encapsulated in the method body, the external exposure method name.
Advantages: 1) hide implementation details 2) increase the reusability of the code
to call: SELECT function name (argument list) [FROM table];
Features:
1) What is called (function name)
2) what (function Function)
Category:
1) one-way function
such CONCAT (str1, str2, ...) , LENGTH (str), IFNULL (expr1, expr2) , etc.
2) grouping function
function: for statistical use, also known as statistical functions, aggregate functions, the set of functions
* /
* *
4.1 One-way function
**
# A, character function
. 1 #. The LENGTH (STR): Get the number of bytes of the parameter values
SELECT LENGTH('John');
SELECT LENGTH('肖战hahaha');
SHOW VARIABLES LIKE '%CHAR%'
2 #. CONCAT (str1, str2, ...) string concatenation
SELECT CONCAT(last_name,'_',first_name) as 姓名
FROM employees;
#3.upper、LOWER(str)
SELECT UPPER('john');
SELECT LOWER('JOHN');
# Example: change the name in uppercase, lowercase name change, then splicing
SELECT CONCAT(upper(last_name),'_',LOWER(first_name)) as 姓名
FROM employees;
#4.SUBSTR(str FROM pos FOR len)、SUBSTRING(str FROM pos FOR len)
# Note: The index starts at 1
# Intercept all from behind the character at the specified index
SELECT SUBSTR('博君一肖是真的',5) as output;
# Gets the character length of the character from the specified index
SELECT SUBSTR('博君一肖是真的',1,4) as output;
Case #: Name of the first character uppercase, lowercase other characters, and then show up with a mosaic _
SELECT CONCAT(UPPER(SUBSTR(last_name,1,1)),'_',lower(substr(last_name,2))) as out_put
FROM employees;
5 #. INSTR (str, substr): Returns the index of the first occurrence of the substring, and returns 0 if not found
SELECT INSTR('博君一肖是真的','是真的');
#6.TRIM([remstr FROM] str)
SELECT LENGTH(TRIM(' 博君一肖 ')) as out_put;
SELECT TRIM('g' from 'ggggggg博君一肖ggggg是真的gggggg') as out_put;
. # 7 LPAD : left pad character length achieved with the specified characters
SELECT LPAD('博君一肖',9,'*') as out_put;
. 8 #. RPAD : right padding character length used to achieve the specified character
SELECT rPAD('博君一肖',9,'*') as out_put;
9 #. REPLACE Replace
SELECT REPLACE('博君一肖是真的','博君一肖','肖战&王一博') as out_put;
# Second, mathematical functions
. # 1 ROUND (the X-): rounding
SELECT ROUND(-1.55);
SELECT ROUND(1.568,2);
2 #. Ceil : rounding up, returns> = the smallest integer parameter
SELECT CEIL(1.003);
. #. 3 the FLOOR (X-): rounding down, returns <= the maximum integer parameter
SELECT FLOOR(-9.99);
4 #. TRUNCATE : truncates
SELECT TRUNCATE(1.9595,2)
. 5 #. The MOD (N, M): modulo
SELECT MOD(10,3);
SELECT 10%3;
**
Third, the date function
**
# 1. The NOW : Returns the current system date and time
SELECT NOW();
2 #. CURDATE : Returns the current system date and time is not included
SELECT CURDATE();
3 #. Curtime : Returns the current system time, no date
SELECT CURTIME();
# 4 can obtain a specified part: year, month, day, hour, minute, second
SELECT YEAR(NOW()) AS 年;
SELECT YEAR('1996-9-8') as 年;
SELECT YEAR(hiredate) as 年 FROM employees;
SELECT MONTH(NOW()) AS 月;
SELECT MONTHNAME(NOW()) AS 月;#显示英文月份
. 5 #. STR_TO_DATE (STR, the format): by date characters into the specified format
SELECT STR_TO_DATE('1996-9-8','%Y-%c-%d') as out_put;
Case #: query entry date 1992-4-3 employee information
SELECT *
FROM employees
WHERE hiredate=STR_TO_DATE('4-3 1992','%c-%d %Y');
6 #. The DATE_FORMAT (DATE, format): Convert date into characters
SELECT DATE_FORMAT(now(),'%y年%m月%d日') as out_put;
# Query the name of a staff bonus and entry date (January XX / XX day XX years)
SELECT last_name,DATE_FORMAT(hiredate,'%m月/%d日 %y年') as 入职日期
FROM employees
WHERE commission_pct is not null;
**
IV. Other functions
**
SELECT VERSION();
SELECT DATABASE();
SELECT USER();
**
V. Process Control Functions
**
# 1. IF function: if else results
SELECT IF(10<5,'大','小');
SELECT last_name,commission_pct,IF(commission_pct is null,'没奖金,呵呵','有奖金,哈哈') as 备注
FROM employees;
. # 2 the CASE function uses a:
/ *
The CASE field or expression to be determined
WHEN constant value of 1 or 1 THEN statements 1 to be displayed;
value of the constant WHEN 2 THEN statement to display two or 2;
...
value n or ELSE statement to be displayed n;
the END;
* /
* / * Case: Query wage employees, requiring
department number = 30, wages display is 1.1 times;
sector number = 40, wages display is 1.2 times;
sector number = 50, wages display is 1.3 times;
in other sectors, wage display original salary.
/
SELECT salary as 原始工资,department_id,
CASE department_id
WHEN 30 THEN salary*1.1
WHEN 40 THEN salary*1.2
WHEN 50 THEN salary*1.3
ELSE salary
END AS 新工资
FROM employees;
. # 3 the CASE function uses two:
/ *
The CASE
the WHEN conditions a value of 1 or THEN statement to display 1
the WHEN conditions two values 2 THEN statement to be displayed or 2
...
the ELSE to display the value of n or statement n
the END
* /
/
Case: Query wages of employees:
if wages> 20,000, showing A-level;
if wages> 15,000, display B level;
if wages> 10000, C-level display;
otherwise, the 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 工资级别
FROM employees;