SQL -- a common function of DQL

Keywords: Java Database MySQL

concept

Similar to the java method, a group of logic statements are encapsulated in the method body to expose the method name;

Benefits:
1. Hidden implementation details
2. Improve code reuse

Call:
select function name (argument list) [from table];

Characteristic:
① What is the name (function name)
② What to do (function function function)

Classification:
1. Single line function
Such as concat, length, ifnull, etc
2. Grouping function
Function: used for statistics, also known as statistical function, aggregate function and group function

Common functions:

Character function:

length:Get the number of bytes(utf-8 One Chinese character represents three bytes,gbk Is 2 bytes)
concat
substr
instr
trim
upper
lower
lpad
rpad
replace

Mathematical functions:

round
ceil
floor
truncate
mod

Date function:

now
curdate
curtime
year
month
monthname
day
hour
minute
second
str_to_date
date_format

Other functions:

version
database
user

Control function

if
case

1, Character function

1.length gets the number of bytes of parameter value

SELECT LENGTH('john');
SELECT LENGTH('Zhang Sanfeng hahaha');

SHOW VARIABLES LIKE '%char%'

2.concat concatenation string

SELECT CONCAT(last_name,'_',first_name) Full name FROM employees;

3.upper,lower

SELECT UPPER('john');
SELECT LOWER('joHn');
#Example: change last name to uppercase, first name to lowercase, and then concatenate
SELECT CONCAT(UPPER(last_name),LOWER(first_name))  Full name FROM employees;

4.substr,substring

Note: index starts at 1

#Intercepts all characters after the specified index
SELECT SUBSTR('Li Mochou falls in love with Lu Zhanyuan',7)  out_put;

#Intercepts characters of the specified character length from the specified index
SELECT SUBSTR('Li Mochou falls in love with Lu Zhanyuan',1,3) out_put;


#Case: the first character in the name is in uppercase, the other characters are in lowercase, and then they are displayed by splicing

SELECT CONCAT(UPPER(SUBSTR(last_name,1,1)),'_',LOWER(SUBSTR(last_name,2)))  out_put
FROM employees;

5.instr returns the index of the first occurrence of the substring. If it cannot be found, it returns 0

SELECT INSTR('Yangbuyinliuxia regrets falling in love with yinliuxia','Yin eight Xia') AS out_put;

6.trim

Remove white space characters or other predefined characters on both sides of the string;

SELECT LENGTH(TRIM('    Zhang Cu Shan    ')) AS out_put;

SELECT TRIM('aa' FROM 'aaaaaaaaa Zhang aaaaaaaaaaaa Cui Shan aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')  AS out_put;

7.lpad uses the specified characters to fill the left with the specified length

SELECT LPAD('Yin Su Su',2,'*') AS out_put;

8.rpad uses the specified characters to fill the right with the specified length

SELECT RPAD('Yin Su Su',12,'ab') AS out_put;

9.replace

SELECT REPLACE('Zhou Zhiruo Zhou Zhiruo Zhou Zhiruo Zhang Wuji falls in love with Zhou Zhiruo','Zhou Zhi Luo','Zhao Min') AS out_put;

2, Mathematical function

Round round

SELECT ROUND(-1.55);
SELECT ROUND(1.567,2);

ceil rounds up to return > = the minimum integer of the parameter

SELECT CEIL(-1.02);

floor rounds down to return the maximum integer of the parameter

SELECT FLOOR(-9.99);

truncate

SELECT TRUNCATE(1.69999,1);

mod surplus

/*
mod(a,b) :   a-a/b*b
mod(-10,-3):-10- (-10)/(-3)*(-3)=-1
*/

SELECT MOD(10,-3);
SELECT 10%3;

3, Date function

now returns the current system date + time

SELECT NOW();

curdate returns the current system date, excluding time

SELECT CURDATE();

curtime returns the current time, excluding the date

SELECT CURTIME();

Can get the specified part, year, month, day, hour, minute, second

SELECT YEAR(NOW()) year;
SELECT YEAR('1998-1-1') year;

SELECT  YEAR(hiredate) year FROM employees;

SELECT MONTH(NOW()) month;
SELECT MONTHNAME(NOW()) month;

STR? To? Date converts a character to a date in the specified format

SELECT STR_TO_DATE('1998-3-2','%Y-%c-%d') AS out_put;

Query the employee information with the date of entry 1992-4-3

SELECT * FROM employees WHERE hiredate = '1992-4-3';

SELECT * FROM employees WHERE hiredate = STR_TO_DATE('4-3 1992','%c-%d %Y');

Date? Format converts a date to a character

SELECT DATE_FORMAT(NOW(),'%y year%m month%d day') AS out_put;

Query the name of the employee with bonus and the date of employment (xx month / xx day, xx year)

SELECT last_name,DATE_FORMAT(hiredate,'%m month/%d day %y year') Date of entry
FROM employees
WHERE commission_pct IS NOT NULL;

4, Other functions

SELECT VERSION();
SELECT DATABASE();
SELECT USER();

5, Process control function

1.if function: the effect of if else

SELECT IF(10<5,'large','Small');

SELECT last_name,commission_pct,IF(commission_pct IS NULL,'No bonus, hehe','With bonus, hee hee') Remarks
FROM employees;

2. Use of case function I: the effect of switch case

/*
java in
switch(Variable or expression){
	case Constant 1: statement 1;break;
	...
	default:Statement n;break;

}

mysql in

case Field or expression to judge
when Value 1 or statement 1 to be displayed by constant 1 then;
when Value 2 or statement 2 to be displayed by constant 2 then;
...
else The value n or statement n to be displayed;
end
*/

/*Case: inquire the salary of employees, and ask for

Department No. = 30, display salary is 1.1 times
 Department number = 40, showing 1.2 times of salary
 Department No. = 50, display salary is 1.3 times
 For other departments, the displayed salary is the original salary

*/


SELECT salary Original wages,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 New wages
FROM employees;

3. Use of case function II: similar to multiple if

/*
java:
If (condition 1) {
	Statement 1;
}Else if (condition 2){
	Statement 2;
}
...
else{
	Statement n;
}

mysql:

case 
when condition 1 then value 1 or statement 1 to display
 when condition 2 then value 2 or statement 2 to display
. . . 
Value n or statement n to be displayed by else
end
*/

#Case: query the salary of employees
 If salary > 20000, display level A
 Display level B if salary > 15000
 Display level C if salary > 10000
 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 salary level
FROM employees;

Published 65 original articles, won praise 0, visited 623
Private letter follow

Posted by djjamiegee on Sun, 12 Jan 2020 02:28:34 -0800