MySQL函数学习

常见函数:

进阶4:常见函数
        一、单行函数
        1、字符函数
               concat拼接
               substr截取子串
               upper转换成大写
               lower转换成小写
               trim去前后指定的空格和字符
               ltrim去左边空格
               rtrim去右边空格
               replace替换
               lpad左填充
               rpad右填充
               instr返回子串第一次出现的索引,如果找不到就返回0
               length 获取字节个数
               ifnull 判断是否为空
查看字符集编码show variables like ‘%char%‘;
mysql> select concat(upper(last_name),lower(first_name)) from employees;函数可以嵌套函数
mysql> select substr(‘李莫愁爱上了陆展元‘,7,3) out_put;索引从1开始,3表示截取长度
+-----------+
| out_put   |
+-----------+
| 陆展元    |
+-----------+
 
mysql> select instr(‘杨不悔爱上了殷六侠‘,‘殷六侠‘) out_put ;
+---------+
| out_put |
+---------+
|       7 |
+---------+
 
mysql> select (trim(‘aa‘ from ‘aaaaaaaa   abc aaaaaa ‘)) as out_put;
+----------------+只会去掉开头和结尾的
| out_put        |
+----------------+
|    abc aaaaaa  |
+----------------+
mysql> select lpad(‘aa‘,10,‘bb‘);
+--------------------+
| lpad(‘aa‘,10,‘bb‘) |
+--------------------+
| bbbbbbbbaa         |
+--------------------+
mysql> select replace(‘abcd‘,‘ab‘,‘ll‘);
+---------------------------+
| replace(‘abcd‘,‘ab‘,‘ll‘) |
+---------------------------+
| llcd                      |
+---------------------------+
1 row in set (0.00 sec)
        2、数学函数,第二个参数都是小数位数
               round 四舍五入
               rand 随机数
               floor向下取整
               ceil向上取整>=该数的整数
               mod取余
               truncate截断
mysql> select round(-1.55);
+--------------+
| round(-1.55) |
+--------------+
|           -2 |
+--------------+
mysql> select round(-1.55,1);保留的小数位数
+----------------+
| round(-1.55,1) |
+----------------+
|           -1.6 |
+----------------+
mysql> select ceil(1.01);
+------------+
| ceil(1.01) |
+------------+
|          2 |
+------------+
mysql> select ceil(-1.02);
+-------------+
| ceil(-1.02) |
+-------------+
|          -1 |
+-------------+
mysql> select truncate(-1.02,1);
+-------------------+
| truncate(-1.07,1) |
+-------------------+
|              -1.0 |
+-------------------+
1 row in set (0.00 sec)
 
mysql> select mod(10,-3);
+------------+
| mod(10,-3) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)
 
mysql> select mod(-10,-3);
+-------------+
| mod(-10,-3) |
+-------------+
|          -1 |
+-------------+
3、日期函数
               now当前系统日期+时间
               curdate当前系统日期
               curtime当前系统时间
               str_to_date 将字符转换成日期
               date_format将日期转换成字符
        
 
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2018-04-08 13:55:52 |
+---------------------+
1 row in set (0.02 sec)
 
mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2018-04-08 |
+------------+
1 row in set (0.00 sec)
 
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 13:57:08  |
+-----------+
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
|        2018 |
+-------------+
1 row in set (0.00 sec)
 
mysql> select curtime(‘1998-1-1‘);
mysql> select year(‘1998-1-1‘);//month
+------------------+
| year(‘1998-1-1‘) |
+------------------+
|             1998 |
+------------------+
mysql> select month(‘1998-1-1‘);
+-------------------+
| month(‘1998-1-1‘) |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.02 sec)
 
mysql> select monthname(‘1998-1-1‘);
+-----------------------+
| monthname(‘1998-1-1‘) |
+-----------------------+
| January               |
 
日期字符转换


mysql> select str_to_date(‘1998-3-2‘,‘%Y-%c-%d‘) as out_put;
+------------+
| out_put    |
+------------+
| 1998-03-02 |
+------------+
mysql> select* from employees where hiredate=str_to_date(‘4-3 1992‘,‘%c-%d %Y‘);
 
mysql> select date_format(now(),‘%Y年%c月%d日‘) as out_put;
+------------------+
| out_put          |
+------------------+
| 2018年4月08日    |
+------------------+
查询有奖金的员工名和入职日期(XX月/XX日 XX年)
mysql> select last_name,date_format(hiredate,‘%c月%d日 %Y年‘) from employees where commission_pct is not null;
+------------+------------------------------------------+
| last_name  | date_format(hiredate,‘%c月%d日 %Y年‘)    |
+------------+------------------------------------------+
| Russell    | 12月23日 2002年                          |
 
4、流程控制函数
               if 处理双分支
               case语句 处理多分支
               case 判断的字段或表达式
                       when情况1 then语句1
                       when情况2 then语句n
                       …
                       Else 语句n
               end
  mysql> select if(10>5,‘大‘,‘小‘) as out_put;
+---------+
| out_put |
+---------+
| 大      |
+---------+
 
mysql> select last_name,commission_pct,if(commission_pct is null,‘没奖金,呵呵‘,‘有奖金,嘻嘻‘) from employees out_put;
+-------------+----------------+----------------------------------------------------------------------+
| last_name   | commission_pct | if(commission_pct is null,‘没奖金,呵呵‘,‘有奖金,嘻嘻‘)             |
+-------------+----------------+----------------------------------------------------------------------+
| K_ing       |           NULL | 没奖金,呵呵                                                         |
 
查询员工工资,要求
部门号=30,显示工资的1.1倍
部门号=40,显示工资的1.2倍
部门号=50,显示工资的1.3倍
其他部门,显示工资为原工资
mysql> select last_name,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_salary
-> from employees;
+-------------+----------+---------------+------------+
| last_name   | salary   | department_id | new_salary |
+-------------+----------+---------------+------------+
| K_ing       | 24000.00 |            90 |   24000.00 |
| Kochhar     | 17000.00 |            90 |   17000.00 |
 
 
查询员工工资情况
如果工资>20000,显示A级别
如果工资>15000,显示B级别
如果工资>10000,显示C级别
否则,显示D级别
mysql> select last_name,salary,department_id, case 
when salary>20000 then ‘A‘ 
when salary>15000 then ‘B‘ 
when salary>10000 then ‘C‘ 
else ‘D‘ end as new_salary 
from employees 
order by department_id asc;
+-------------+----------+---------------+------------+
| last_name   | salary   | department_id | new_salary |
+-------------+----------+---------------+------------+
| Grant       |  7000.00 |          NULL | D          |
| Whalen      |  4400.00 |            10 | D          |
| Hartstein   | 13000.00 |            20 | C          |
 
5、其他函数
               version版本
               database当前库
               user当前连接用户
 
二、分组函数,统计,组函数,聚合函数
 
 
               sum 求和
               max 最大值
               min 最小值
               avg 平均值
               count 计数
        
               特点:
               1、以上五个分组函数都忽略null值,除了count(*)
               2、sum和avg一般用于处理数值型
                       max、min、count可以处理任何数据类型
            3、都可以搭配distinct使用,用于统计去重后的结果
               4、count的参数可以支持:
                       字段、*、常量值,一般放1
        
                  建议使用 count(*)

mysql> select sum(salary) from employees;

+-------------+

| sum(salary) |

+-------------+

|   691400.00 |

+-------------+

1 row in set (0.00 sec)

mysql> select avg(salary) from employees;

+-------------+

| avg(salary) |

+-------------+

| 6461.682243 |

+-------------+

1 row in set (0.00 sec)

mysql> select min(salary) from employees;

+-------------+

| min(salary) |

+-------------+

|     2100.00 |

+-------------+

1 row in set (0.00 sec)

mysql> select max(salary) from employees;

+-------------+

| max(salary) |

+-------------+

|    24000.00 |

+-------------+

1 row in set (0.00 sec)

mysql> select count(salary) from employees;

+---------------+

| count(salary) |

+---------------+

|           107 |

+---------------+

1 row in set (0.00 sec)

原文地址:https://www.cnblogs.com/BetterThanEver-WWCH/p/8744881.html

时间: 2024-08-09 05:34:33

MySQL函数学习的相关文章

MySQL函数学习笔记二:字符函数

1. 计算字符串字符数和字符串长度 - CHAR_LENGTH(s) CHAR_LENGTH(str): 返回str所包含的字符个数. mysql> select CHAR_LENGTH('MySQL'); +----------------------+ | CHAR_LENGTH('MySQL') | +----------------------+ |                    5 | +----------------------+ 2. 合并字符 - CONCAT(s1,s

MySQL函数学习笔记一:数学函数

1. ABS(x): 返回x的绝对值 mysql> select ABS(1), ABS(-1), ABS(0); +--------+---------+--------+ | ABS(1) | ABS(-1) | ABS(0) | +--------+---------+--------+ |      1 |       1 |      0 | +--------+---------+--------+ 2. PI(): 返回圆周率 mysql> select PI(); +-----

mysql 函数学习

1.时间函数 SELECT CURRENT_DATE() AS date, CURRENT_TIME() AS `time`, CURRENT_TIMESTAMP() AS `timestamp`; -- 输出 -- +------------+----------+---------------------+ -- | date | time | timestamp | -- +------------+----------+---------------------+ -- | 2019-0

MySQL基础学习之函数

数学函数 绝对值      abs() 圆周率      PI() 平方根 sqrt() 模除取余   mod(被除数,除数) 随机数      rand() 四舍五入    round(数字) 次方         pow(5,2) e为底的指数函数  exp(数字) 字符串函数 字符长度       length(字符) 字符连接       concat(字符1,字符2) 带符号的字符连接    concat_ws('-','su','zhou') 字符插入      insert('su

【转】MYSQL入门学习之十三:自定义函数的基本操作

转载地址:http://www.2cto.com/database/201212/177382.html 一.自定义函数(UDF)的特性和功能  www.2cto.com 函数能分返回字符串,整数或实数; 可以定义一次作用于一行的简单函数,或作用于多行的组的集合函数; 二.基本操作 1.创建自定义函数 CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL} BEGIN //函数实现的语句 END; aggre

MySQL学习笔记10(MySQL函数)

MySQL学习笔记10 MySQL函数 MySQL数据库中提供了很丰富的函数.MySQL函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数.格式化函数等.通过这些函数,可以简化用户的操作.SELECT语句及其条件表达式都可以使用这些函数.同时,INSERT.UPDATE.DELECT语句及其条件表达式也可以使用这些函数. 1:数学函数 数学函数是M有SQL中常用的一类函数.主要用于处理数字,包括整型.浮点数等.数学函数包括绝对值函数.正弦函数.余弦函数.获取随机数

mysql 函数和存储过程的学习

#创建存储子程序需要CREATE ROUTINE权限. #· 提醒或移除存储子程序需要ALTER ROUTINE权限.这个权限自动授予子程序的创建者. #· 执行子程序需要EXECUTE权限.然而,这个权限自动授予子程序的创建者.同样,子程序默认的SQL SECURITY 特征是DEFINER,它允许用该子程序访问数据库的用户与执行子程序联系到一起 #-------------------------------------------------------------------------

mysql 时间和日期函数学习

mysql 时间和日期函数学习 为了方便使用了比较智能的mysql客户端工具mycli,关于这个工具的介绍可以在另一篇文章(http://watchman110.blog.51cto.com/9194028/1687953)里了解! 1,now()函数,获得当前日期和时间 mysql [email protected]:(none)> SELECT NOW() +---------------------+ | NOW()       | |---------------------| |201

MYSQL数据库学习----MYSQL函数

MYSQL函数分为几种 数学函数 字符串函数 日期和时间函数 条件判断函数 系统信息函数 加密函数 格式化函数 一:数学函数 主要使用的几个数学函数 1 ABS()----绝对值函数 eg. SELECT ABS(-5); 返回 5 2 PI()----圆周率函数 eg. SELECT PI(); 返回 3.141596 3 SIGN()----符号函数 正数返回1,0返回0,负数返回-1 eg. SELECT SIGN(5),SIGN(0),SIGN(-5); 返回 1,0,-1 4 RAND