Use select to test functions directly: For example, select 'concat';
here are some commonly used functions in Mysql:
1. concat()
Function: splice the parameters inside concat together
- Example: Query the employee's name and salary, the required salary unit is yuan:
select ename,concat(sal,'元') 工资 from emp;
2. Numerical calculation: + - * / %
(mod(7,2) is equivalent to 7%2)
- Example: Query the unit price, inventory and total value of each product in the product table (unit price * inventory)
select price,num,price * num '总价值' from t_item;
- Example: Query the name, salary, and year-end bonus of each employee in the employee table (five months' salary)
select ename,sal,sal * 5 '年终奖' from emp;
3. Date related functions:
- Get year, month, day, hour, minute and second:
select now()
- Get the current date:
select curdate()
- Get the hour, minute and second of the current time:
select curtime()
- Extract the year, month, and day from the year, month, day, hour, minute, and second:
select date(now())
- Extract hour, minute, and second from year, month, day, hour, minute, and second:
select time(now())
- Extract time components from year, month, day, hour, minute, and second (year, month, day, hour, minute, and second):
extract()
- Year of extraction:
select extract(year from now());
- Extract month:
select extract(month from now());
- Withdrawal date:
select extract(day from now());
- When extracting:
select extract(hour from now());
- Extract points:
select extract(minute from now());
- Extract seconds:
select extract(second from now());
- Year of extraction:
- Example: Query the names and entry years of all employees in the employee table:
select ename,extract(year from hiredate) from emp;
4. Date formatting:date_format(时间,格式)
-
%Y
: Four years%y
,: Two years -
%m
: Two months%c
,: one month -
%d
:day, -
%H
: 24-hour system,:%h
12-hour system -
%i
:Minute -
%s
:second -
Example: Convert the current time to format:
select date_format(now(),'%Y年%m月%d日%H时%i分%s秒');
-
Example: query product name and product creation date (format: x year x month x day)
select title,date_format(date(curdate_time),'%Y年%m月%d日') from t_item;
select title,date_format(curdate_time,'%Y年%m月%d日') from t_item;
5. Convert a non-standard date string into a standard time format:str_to_date(时间字符串,字符串格式)
- Example: 14.08.2018 08:00:00:
select str_to_date('14.08.2018 08:00:00','%m.%d.%Y %H:%i:%s');
6. ifnull(x,y)
Function:
Explanation: age=ifnull(x,18) if the value of x is null then age=18, if not null then age=x;
- Example: Modify the value of the bonus as null in the employee table to 0:
update emp set comm = ifnull(comm,0);
7. Aggregate function: used for statistics of multiple data
- Summation:
sum(字段名)
- Example: Query the total salary of department 10 in the emp table:
select sum(sal) from emp where deptno = 10;
- Example: Query the total salary of department 10 in the emp table:
- average value:
avg(字段名)
- Example: Query the average wage of all employees in the emp table:
select avg(sal) from emp;
- Example: Query the average wage of all employees in the emp table:
- Maximum value:
max(字段名)
- Example: Query the highest bonus of employees in department 30:
select max(comm) from emp where deptno = 30;
- Example: Query the highest bonus of employees in department 30:
- Minimum:
min(字段名)
- Example: Query the unit price of the cheapest product in the product table:
select min(price) from t_item;
- Example: Query the unit price of the cheapest product in the product table:
- Statistics: used in
count(数量名)
generalcount(*)
- Example: Count how many people are in department 30:
select count(*) from emp where deptno = 30;
- Example: Count how many people are in department 30:
8. Functions related to strings: (Small labels start from 1, generally including the head but not the tail)
-
Get string length:
char_length(str)
- Example: Get the names of all employees and the length of their name strings:
select ename,char_length(ename) from emp;
- Example: Get the names of all employees and the length of their name strings:
-
Get the position of a string in another string:
instr(str,substr)
select instr('adegc','e');
-
Insert string:
insert(str,start,length,newStr);
select insert('abcdefg',2,4,'m');
-
To uppercase:, To
upper(字符串)
lowercase:lower(字符串)
select upper('nba'),lower('NBA');
-
Intercept on the left:,
left(str,length)
Intercept on the right:right(str,length)
select left('abcdefg',2),right('abcdefg',2);
-
Remove the blanks at both ends:
trim(str)
(The middle can't be removed)select trim(' a b ');
-
Intercept string:
substring(str,截取开始的位置,截取的长度)
(including head but not tail)select substring('abcdefg',3,2);
-
Repetition:
repeat(str,count)
count is the number of repetitionsselect repeat('ab',2);
-
replace:
replace(str,old,new)
select replace('This is mysql','my','your');
-
Reverse:
reverse(str)
select reverse('abc');
9. Mathematical related functions:
- Round down:
floor(num)
select floor(3.48);
- Rounding:
round(num)
,round(num,m)
m represents the number of decimal placesselect round(3.84);
select round(3.8456,3);
- Non-rounding (retain a few decimal places):
truncate(num,m)
select truncate(3.8456,3);
- Get random number: rand() (random number with value range 0-1)
-
Example: Find a random number from 5-10:
select floor(rand()*6)+5;
-
Example: Find a random number from 3-8:
select floor(rand()*6)+3;
-