MySQL 函数使用(入门)

目标

如何在MySQL数据库中创建函数(Function)

语法

CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,参数是可选的
RETURNS type
[ characteristic ...] routine_body
  • CREATE FUNCTION 用来创建函数的关键字;
  • func_name 表示函数的名称;
  • func_parameters为函数的参数列表,参数列表的形式为:[IN|OUT|INOUT] param_name type
  1. IN:表示输入参数;
  2. OUT:表示输出参数;
  3. INOUT:表示既可以输入也可以输出;
  4. param_name:表示参数的名称;
  5. type:表示参数的类型,该类型可以是MySQL数据库中的任意类型;

示例

创建示例数据库、示例表与插入样例数据脚本:

create database hr;
use hr;

create table employees
(
	employee_id int(11) primary key not null auto_increment,
	employee_name varchar(50) not null,
	employee_sex varchar(10) default '男',
	hire_date datetime not null default current_timestamp,
	employee_mgr int(11),
	employee_salary float default 3000,
	department_id int(11)
);

insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('David Tian','男',10,7500,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Black Xie','男',10,6600,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Moses Wang','男',10,4300,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Rena Ruan','女',10,5300,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Sunshine Ma','女',10,6500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Scott Gao','男',10,9500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Warren Si','男',10,7800,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Kaishen Yang','男',10,9500,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Simon Song','男',10,5500,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Brown Guan','男',10,5000,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Eleven Chen','女',10,3500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Cherry Zhou','女',10,5500,4);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Klause He','男',10,4500,5);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Maven Ma','男',10,4500,6);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Stephani Wang','女',10,5500,7);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Jerry Guo','男',10,8500,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Gerardo Garza','男',10,25000,8);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Derek Wu','男',10,5500,5);

select * from employees;

创建函数-根据ID获取员工姓名与员工工资

DELIMITER //
CREATE FUNCTION GetEmployeeInformationByID(id INT)
RETURNS VARCHAR(300)
BEGIN
	RETURN(SELECT CONCAT('employee name:',employee_name,'---','salary: ',employee_salary) FROM employees WHERE employee_id=id);
END//
DELIMITER ;

调用函数

在MySQL——函数的使用方法与MySQL内部函数的使用方法一样。

<更多精彩内容,见后面更新...>

如果您们在尝试的过程中遇到什么问题或者我的代码有错误的地方,请给予指正,非常感谢!

联系方式:[email protected]

版权@:转载请标明出处!

时间: 2024-12-19 11:27:36

MySQL 函数使用(入门)的相关文章

马哥教育第二十三MySQL基础应用入门

1.MySQL基础应用入门                安装mariadb-10.0.19:                      vim /usr/local/mariadb-10.0.19-linux-x86_64/INSTALL-BINARY                                               cd /usr/local                      ln -sv mariadb-10.0.19-linux-x86_64 mysql

MySql概述及入门(五)

MySql概述及入门(五) MySQL集群搭建之读写分离 读写分离的理解 为解决单数据库节点在高并发.高压力情况下出现的性能瓶颈问题,读写分离的特性包括会话不开启事务,读语句直接发送到 salve 执行.基本的原理是让主数据库处理事务性增.改.删操作(INSERT.UPDATE. DELETE),而从数据库处理SELECT查询操作.数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库. 为什么使用读写分离 因为数据库的“写”(写10000条数据到oracle可能要3分钟)操作是比较耗时的

mysql函数大全

mysql函数大全 对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NULL. mysql> select ASCII('2');    -> 50mysql> select ASCII(2);    -> 50mysql> select ASCII('dx');    -> 100也可参见ORD()函数. ORD(str) 如果字符串str最

mysql函数

一.字符串函数 (1).计算字符串个数 语法:char_length(string) 例子:select char_length(user_name) from gonda; (2).计算字节长度 语法:length(string) 例子:select length(name) from student; (3).合并字符串函数 语法:concat(s1,s2) 默认不定义分割符号 举例:select concat(user_id,user_name) from gonda; 语法:concat

第二百八十五节,MySQL数据库-MySQL函数

MySQL数据库-MySQL函数 1.MySQL内置函数 SELECT执行函数,后面跟要执行的函数 CHAR_LENGTH(str)函数:返回字符串的字符长度 -- CHAR_LENGTH(str)函数:返回字符串的字符长度 SELECT CHAR_LENGTH('欢迎光临'); LENGTH(str)函数:返回字符串的字节长度 -- LENGTH(str)函数:返回字符串的字节长度 SELECT LENGTH('欢迎光临'); CONCAT(str1,str2,...)函数:拼接字符串 --

Mysql 函数使用记录(一)——DATEDIFF、CONCAT

当目前为止呢,个人对Mysql的函数没有进行过统一的学习使用,都是用到了再去学习.而近日开始学习Linux了,所以为了防止这段时期结束后,将此阶段期间遇到的Mysql函数遗忘,开始在此对其做一个简单的记录. 昨天下班前朋友呢让帮忙看一个sql,是关于生日提醒的,实际应用中呢是要实现提前一天提醒用户生日,而sql呢是用来查询后一天要过生日的用户.在她原来的sql中用到了CONCAT.DATEDIFF函数,这两个函数呢我自个儿呢之前是没有用到过,所以在解决完问题之后在此记录一下用法及思路. 首先,来

php总结8——mysql函数库、增删改

8.1 mysql函数库 php的函数   .php中用来操作mysql函数库的函数 常用函数 mysql_connect("主机名称/ip","用户名","密码")--->建立php到mysql的连接,返回资源----> mysql_error()  返回上一个mysql操作的文本错误信息 @ 错误抑制符 一般来说,mysql_connect和mysql_error() 一起使用,用如下方式表示: <?php $link[em

【转】mysql函数

MySQL函数 MySQL数据库提供了很多函数包括: 数学函数: 字符串函数: 日期和时间函数: 条件判断函数: 系统信息函数: 加密函数: 格式化函数: 一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. 函数 作用 ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 SELECT CEIL(1.5) -- 返回2 FLOOR(x) 返回小于或等于x的最大整数 SELECT FLOOR(1.5) 

mysql 函数库

函数库 mysql存储过程基本函数包括:字符串类型,数值类型,日期类型 一.字符串类 CHARSET(str) //返回字串字符集 CONCAT (string2 [,… ]) //连接字串 INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0 LCASE (string2 ) //转换成小写 LEFT (string2 ,length ) //从string2中的左边起取length个字符 LENGTH (string )