MySql函数

1.MySql函数

1.内置函数

(1).数字类

语句 含义
select rand() from dual; 随机数
select * from stuinfo order by rand(); 随机排序
select round(5.6); 四舍五入
select ceil(5.3); 向上取整
select floor(5.6); 向下取整

(2).大小写转换

语句 含义
select ucase(‘i am lyb’); 大写
select lcase(‘I AM LYB’); 小写

(3).截取字符串

语句 含义
select left(‘abcdefg’,3); 截取左边的3位
select right(‘abcdefg’,3); 截取右边3位
select substring(‘abcdefg’,2,3); 从第2位开始取3个字符,起始位置从1开始

(4).字符串拼接

select concat(sid,sname,age,sex,city) from stuinfo;

mysql> select concat(sid,sname,age,sex,city) from stuinfo;
+--------------------------------+
| concat(sid,sname,age,sex,city) |
+--------------------------------+
| 7小明18male上海                      |
| 8小刚20male北京                       |
| 9小强22male重庆                      |
| 10小力23male天津                      |
| 11小丽21female北京                    |
| 12小月20female天津                    |
| 13小yb18male重庆                    |
| 17百强18male黑龙江                      |
| 18百强118male黑龙江                     |
| 19百强218male黑龙江                     |
+--------------------------------+

(5).coalesce(str1,str2):如果str1不为null则显示str1,否则显示str2

select sid,sname,coalesce(ch,'缺考'),coalesce(math,'缺考') from stuinfo left join stumarks using(sid);

mysql> select sid,sname,coalesce(ch,'缺考'),coalesce(math,'缺考') from stuinfo l
eft join stumarks using(sid);
+-----+-------+---------------------+-----------------------+
| sid | sname | coalesce(ch,'缺考') | coalesce(math,'缺考') |
+-----+-------+---------------------+-----------------------+
|  11 | 小丽  | 100                 | 80                    |
|   8 | 小刚  | 60                  | 98                    |
|  10 | 小力  | 50                  | 51                    |
|   9 | 小强  | 67                  | 88                    |
|   7 | 小明  | 88                  | 10                    |
|  12 | 小月  | 96                  | 97                    |
|  17 | 百强  | 缺考                | 缺考                   |
|  18 | 百强1 | 缺考                | 缺考                  |
|  19 | 百强2 | 缺考                | 缺考                  |
+-----+-------+---------------------+-----------------------+

(6).length(字节长度)、char_length(字符长度)、trim(去两边空格)、repace(替换)

select length('千锋');

select char_length('千锋');

select length(trim(' 千锋 '));

select replace('pgone','one','two');

(7).时间戳

select unix_timestamp();

(8).将时间戳转成当前时间

select from_unixtime(unix_timestamp());

(9).获取当前时间

select now(),year(now()),month(now()),day(now()),hour(now()), minute(now()),second(now())\G

#现在时间,年,月,日,时,分,秒

(10).dayname(),monthname(),dayofyear()

select dayname(now()) as `星期`,monthname(now()) as `月份`,dayofyear(now()) as `本年第几天`;

(11).datediff(结束日期,开始日期)

例题计算自己活了多少天
select datediff(now(),'1970-1-1');

(12).md5():md5加密

select md5('@123456.');

2.自定义函数

#语法:
Create function 函数名(形参) returns 返回的数据类型
begin
    //函数体
end
#第一步
delimiter //

#不带参数的函数
create function myfun() returns varchar(32)
begin
     return '千锋python';
end //

#调用函数
select myfun()//
#带参数
create function myfun_1(num1 int,num2 int) returns int
begin
     declare num int default 0;
     set num=num1+num2;
     return num;
end //

select myfun_1(100,200)//


#删除函数
drop function myfun_1//

你可能感兴趣的:(MySql函数)