#Advanced 4: Common functions-one-line functions
Syntax: select function name (actual parameter list) [from table]
Features:
1. What is it called (function name)
2. What is it (function)
Classification:
1. Single-line functions
such as concat, length, ifnull...
2. Grouping functions
Function: for statistical use, also known as statistical function, aggregate function, group function
#-------------------------------
#一,Character function
#1.length Get the number of bytes of the parameter value
SELECT LENGTH('john');
SELECT LENGTH('张三丰hahaha');
#2.concat concatenating strings
SELECT CONCAT(last_name,'_',first_name) 姓名 FROM employees;
#3.upper,lower
SELECT UPPER('young');
#Case: Change the last name to uppercase and the first name to lowercase, and then stitch
SELECT CONCAT(UPPER(last_name),'_',LOWER(first_name)) 姓名 FROM employees;
#4.substr,substring index starts from 1
#Intercept all characters from the specified index
SELECT SUBSTR('港港爱霜霜',3) 结果
#Intercept the character of the specified character length from the specified index
SELECT SUBSTR('港港爱霜霜',1,2) 结果
#Case: The first character in the name is capitalized, the other characters are lowercased and then spliced with _ to display it
SELECT CONCAT(UPPER(SUBSTR(last_name,1,1)),'_',LOWER(SUBSTR(last_name,2))) FROM employees;
#5.instr returns the index of the first occurrence of the string, if not found, return 0
SELECT INSTR('港港今年想上岸上海理工','上海理工') AS out_put;
#6.trim
SELECT LENGTH(TRIM(' 上海理工 ')) AS out_put;
SELECT TRIM('a' FROM 'aaaaa上海aaaa理工aaaaa') AS out_put;
#7.lpad Use the specified characters to achieve left padding and specify the length.
# Similarly, rpad is right padding
SELECT LPAD('浙江大学',6,'*') AS out_put;
#8.replace Replace
SELECT REPLACE('港港今年北语考北语','北语','上海理工') AS out_put;
#二, Mathematical Function
#round Rounding
SELECT ROUND(-1.55);
SELECT ROUND(1.239,2);
#ceil Round up, and return the smallest integer greater than or equal to the parameter #Similar floor round
down
SELECT CEIL(1.002);
#truncate Truncate
SELECT TRUNCATE(1.6999,1);
#mod取余
SELECT MOD(10,3);
SELECT 10%3;
#三,date function
#now current date+time
SELECT NOW();
#curdate returns the current date, not including time
SELECT CURDATE();
#The same, CURTIME returns the current time, excluding the date
#Can get the specified part, year, month, day, hour, minute, second
SELECT YEAR(hiredate) FROM employees;
#同理:month,day...
#str_to_date Convert characters to dates in the specified format
SELECT STR_TO_DATE('7-12-2018','%c-%d-%Y');
#Case: Query the information of employees whose entry date is April 3, 1992
SELECT * FROM employees WHERE hiredate=STR_TO_DATE('4-3 1992','%c-%d %Y');
#date_format Convert dates to characters
SELECT DATE_FORMAT(NOW(),'%y年%m月%d日');
#Case: Query the names and entry dates of employees with bonuses (xx month/xx day xx year)
SELECT last_name,DATE_FORMAT(hiredate,'%m月/%d日 %y年')
FROM employees
WHERE commission_pct IS NOT NULL;
#四, other functions
SELECT VERSION();
SELECT DATABASE();
SELECT USER();
#五,Flow control function
#1.if
SELECT last_name,commission_pct,IF(commission_pct,'有奖金','无奖金')
FROM employees;
#2.case #Use
one: the effect of switch case
Syntax:
case field or expression to be judged
when constant 1 then value 1 or statement 1
to be displayed ; when constant 2 then value 2 or statement 2 to be displayed;
…
else The value or statement to be displayed
end
Case: Query the salary of an employee, requiring
department number=30, showing that the salary is 1.1 times the
department number=40, showing that the salary is 1.2 times the
department number=50, showing that the salary is 1.3 times
SELECT salary 原始工资,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;
#Use two: similar to multiple if
syntax:
case
when condition 1 then value 1 or statement 1
when condition 2 then value 2 or statement 2
…
else value or statement to display
end
Case: Query the salary of an employee.
If salary>20000, display level A.
If salary>15000, display level B.
If salary>10000, display level C.
Otherwise, display level D
SELECT salary,
CASE
WHEN salary>20000 THEN 'A'
WHEN salary>15000 THEN 'B'
WHEN salary>10000 THEN 'C'
ELSE 'D'
END
AS 工资级别
FROM employees;
#test
SELECT employee_id,last_name,salary,salary*1.2 'now salary' FROM employees;
SELECT last_name,LENGTH(last_name) 姓名长度,SUBSTR(last_name,1,1) 首字符
FROM employees
ORDER BY 首字符;
SELECT
CONCAT(last_name,' ','earns',' ',salary,' ','but',' ','wants',' ',salary*3)
AS 'dream salary'
FROM employees;
SELECT job_id,
CASE job_id
WHEN 'AD_PRES' THEN 'A'
WHEN 'ST_MAN' THEN 'B'
WHEN 'IT_PROG' THEN 'C'
ELSE 'D'
END AS GRADE
FROM employees;