1、自定义函数

(1)定义语法

create function 函数名(参数) returns 返回值类型

begin

         //代码

end 说明:

(1)函数内部可以有各种编程语言的元素:变量,流程控制,函数调用;

(2)函数内部可以有增删改等语句!

(3)但:函数内部不可以有select(或show或desc)这种返回结果集的语句!

(2)调用

跟系统函数调用一样:任何需要数据的位置,都可以调用该函数。

案例1:返回两个数的和

create function sumhe(num1 int,num2 int) returns int

begin

         return num1+num2;

end$案例2:定义一个函数,返回1到n的和。

create function nhe(n int)  returns int

begin

         declare i int default 1;

         declare s int default 0;

         while i<=n do

                  set s=s+i;

                  set i=i+1;

         end while;

         return s;

end$注意点:创建的函数,是隶属于数据库的,只能在创建函数的数据库中使用。

select sumhe(345,34435)$

2、系统函数

(1)数字类

mysql> select rand();//返回0到1间的随机数

mysql>select * from it_goods  order by rand() limit 2;//随机取出2件商品mysql>select  floor(3.9)//输出3

mysql>select  ceil(3.1)//输出4

mysql>select  round(3.5)//输出4四舍五入

select goods_name,round(shop_price) from goods limit 10;(2)大小写转换

mysql> select ucase('I am a boy!') //    --转成大写

mysql> select lcase('I am a boy!') //    --转成小写3)截取字符串

mysql> select left('abcde',3)//                          --从左边截取

mysql> select right('abcde',3) //        --从右边截取

mysql> select substring('abcde',2,3)//   --从第二个位置开始,截取3个,位置从1开始

select left(goods_name,1),round(shop_price) from goods limit 10

1、自定义函数

(1)定义语法

create function 函数名(参数) returns 返回值类型

begin

         //代码

end 说明:

(1)函数内部可以有各种编程语言的元素:变量,流程控制,函数调用;

(2)函数内部可以有增删改等语句!

(3)但:函数内部不可以有select(或show或desc)这种返回结果集的语句!

(2)调用

跟系统函数调用一样:任何需要数据的位置,都可以调用该函数。

案例1:返回两个数的和

create function sumhe(num1 int,num2 int) returns int

begin

         return num1+num2;

end$案例2:定义一个函数,返回1到n的和。

create function nhe(n int)  returns int

begin

         declare i int default 1;

         declare s int default 0;

         while i<=n do

                  set s=s+i;

                  set i=i+1;

         end while;

         return s;

end$注意点:创建的函数,是隶属于数据库的,只能在创建函数的数据库中使用。

select sumhe(345,34435)$

2、系统函数

(1)数字类

mysql> select rand();//返回0到1间的随机数

mysql>select * from it_goods  order by rand() limit 2;//随机取出2件商品mysql>select  floor(3.9)//输出3

mysql>select  ceil(3.1)//输出4

mysql>select  round(3.5)//输出4四舍五入

select goods_name,round(shop_price) from goods limit 10;(2)大小写转换

mysql> select ucase('I am a boy!') //    --转成大写

mysql> select lcase('I am a boy!') //    --转成小写3)截取字符串

mysql> select left('abcde',3)//                          --从左边截取

mysql> select right('abcde',3) //        --从右边截取

mysql> select substring('abcde',2,3)//   --从第二个位置开始,截取3个,位置从1开始

select left(goods_name,1),round(shop_price) from goods limit 10

mysql> select concat(10,':锄禾日当午')//   --字符串相连select concat(left(goods_name,1),'...'),round(shop_price) from goods limit 10;

mysql> select coalesce(null,123);

coalesce(str1,str2):如果第str1为null,就显示str2

select goods_name,coalesce(goods_thumb,'无图')from goods limit 10;

mysql> select length('锄禾日当午') //   输出10   显示字节的个数

mysql> select char_length('锄禾日当午') // 输出5   显示字符的个数

mysql> select length(trim('  abc  ')) //  trim用来去字符串两边空格

mysql> select replace('abc','bc','pache')//   将bc替换成pache (4)时间类

mysql> select unix_timestamp()//      --时间戳

mysql> select from_unixtime(unix_timestamp()) //  --将时间戳转成日期格式

from_unixtime(unix_timestamp(),'%Y-%m-%d-%h-%i-%d')

mysql>select  curdate();返回今天的时间日期:

mysql> select now()//                                        --取出当前时间

 

 

 

案例1:比如一个电影网站,求出今天添加的电影;在添加电影时,有一个添加的时间戳。

select  id,title  from dede_archives where   (from_unixtime(添加时间,'%Y-%m-%d')=curdate());

MYSQL数据库函数

案例2:比如一个电影网站,求出昨天添加的电影;在添加电影时,有一个添加的时间戳。

扩展,如何取出昨天或者指定某个时间的电影:

date_sub

基本用法:

date_sub(时间日期时间,interval 数字  时间单位)

说明:

(1)时间单位:可以是year month day hour minute second

(2)数字:可以是正数和负数。

比如:取出昨天的日期:

mysql> select date_sub(curdate(),interval 1 day);

比如:取出上一个月日期:

mysql> select date_sub(curdate(),interval 1 month);

如下案例是:求出前第2天添加的电影数据

MYSQL数据库函数

相关文章: