码迷,mamicode.com
首页 > 数据库 > 详细

MySQL自定义函数

时间:2017-03-26 01:16:31      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:str   rem   alt   concat   employee   values   efault   publish   har   

 set names gbk;
DATE_FORMAT(publish_datetime,‘%Y-%m-%d‘);

select date_format(now(),‘%Y年%m月%d日 %H时:%i分:%s秒‘);

技术分享

创建一个现实时间的函数:

 create function f1() returns varchar(30)
 return  date_format(now(),%Y年%m月%d日 %H时%i分%s秒);

 调用这个函数:

 

select f1();

 技术分享

CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一

 

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 TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE 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);

创建函数-根据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 ;  
SELECT GetEmployeeInformationByID(1) ;

 

技术分享

 

MySQL自定义函数

标签:str   rem   alt   concat   employee   values   efault   publish   har   

原文地址:http://www.cnblogs.com/taiguyiba/p/6619027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!