Mysql系统函数

一、concat();

mysql> select concat("张三","太棒了");
+-------------------------+
| concat("张三","太棒了") |
+-------------------------+
| 张三太棒了              |
+-------------------------+
1 row in set (0.00 sec)

mysql> select concat("小明在",now(),"买了一个笔记本");
+-----------------------------------------+
| concat("小明在",now(),"买了一个笔记本") |
+-----------------------------------------+
| 小明在2017-04-29 18:46:17买了一个笔记本 |
+-----------------------------------------+
1 row in set (0.01 sec)

二、case

insert into student values(4,"小狗",12,2,"男"),(5,"张三",12,2,"女"),(6,"大锤",12,1,"男");

select id,age,course,sex,(case when sex = "男" then concat(name,"很帅")
            when sex = "女" then concat(ifnull(sex,name),"很美")           end) as one from student;

+------+------+--------+------+----------+
| id   | age  | course | sex  | one      |
+------+------+--------+------+----------+
|    1 |   18 | 1      | 男   | 小红很帅 |
|    2 |   18 | 2      | 男   | 小强很帅 |
|    3 |   18 | 1      | 女   | 分度很美 |
|    4 |   12 | 2      | 男   | 小狗很帅 |
|    5 |   12 | 2      | 女   | 张三很美 |
|    6 |   12 | 1      | 男   | 大锤很帅 |
+------+------+--------+------+----------+
6 rows in set (0.00 sec)

三、ifnull();

mysql> select ifnull("yiben","erben");
+-------------------------+
| ifnull("yiben","erben") |
+-------------------------+
| yiben                   |
+-------------------------+
1 row in set (0.04 sec)

mysql> select ifnull(null,"yiben");
+----------------------+
| ifnull(null,"yiben") |
+----------------------+
| yiben                |
+----------------------+
1 row in set (0.04 sec)

mysql> select ifnull("","yiben");
+--------------------+
| ifnull("","yiben") |
+--------------------+
|                    |
+--------------------+
1 row in set (0.00 sec)

mysql> select ifnull("null","yiben");
+------------------------+
| ifnull("null","yiben") |
+------------------------+
| null                   |
+------------------------+
1 row in set (0.00 sec)

四、if

if(1,2,3);
1成立吗?成立执行2,不成立执行3;

select if(1>2,"1大于2","1小于2");

mysql> select if(1>2,"1大于2","1小于2");
+---------------------------+
| if(1>2,"1大于2","1小于2") |
+---------------------------+
| 1小于2                    |
+---------------------------+
1 row in set (0.00 sec)

五、字符串截取

substring()

set @a ="abcdefghig";
select substring(@a,2);# 从第二个开始截取到末尾
mysql> select substring(@a,2);
+-----------------+
| substring(@a,2) |
+-----------------+
| bcdefghig       |
+-----------------+
1 row in set (0.05 sec)

substring(@a,2,3);

mysql> select substring(@a,2,3);#从第二个开始截取三个
+-------------------+
| substring(@a,2,3) |
+-------------------+
| bcd               |
+-------------------+
1 row in set (0.00 sec)

select left(@a,2);
select right(@a,2);
select lower(@a);
select upper(@a);

mysql> select left(@a,2);
+------------+
| left(@a,2) |
+------------+
| ab         |
+------------+
1 row in set (0.00 sec)

mysql> select right(@a,2);
+-------------+
| right(@a,2) |
+-------------+
| ig          |
+-------------+
1 row in set (0.00 sec)

mysql> select lower(@a);
+------------+
| lower(@a)  |
+------------+
| abcdefghig |
+------------+
1 row in set (0.00 sec)

mysql> select upper(@a);
+------------+
| upper(@a)  |
+------------+
| ABCDEFGHIG |
+------------+
1 row in set (0.00 sec)

六、数学函数

遗漏知识点: 随机数rand()自己学习

select avg(course) from student;
select sum(course) from student;
select max(course) from student;
select min(course) from student;


mysql> select avg(course) from student;
+--------------------+
| avg(course)        |
+--------------------+
| 1.7142857142857142 |
+--------------------+
1 row in set (0.06 sec)

mysql> select sum(course) from student;
+-------------+
| sum(course) |
+-------------+
|          12 |
+-------------+
1 row in set (0.00 sec)

mysql> select max(course) from student;
+-------------+
| max(course) |
+-------------+
| 3           |
+-------------+
1 row in set (0.00 sec)

mysql> select min(course) from student;
+-------------+
| min(course) |
+-------------+
| 1           |
+-------------+
1 row in set (0.00 sec)

八、日期函数

(1)date_format();

select date_format(now(),"%Y年%M月%d日 %H时%i分%s秒");


mysql> select date_format(now(),"%Y年%M月%d日 %H时%i分%s秒");
+------------------------------------------------+
| date_format(now(),"%Y年%M月%d日 %H时%i分%s秒") |
+------------------------------------------------+
| 2017年April月29日 20时17分00秒                 |
+------------------------------------------------+
1 row in set (0.00 sec)

mysql> select date_format(now(),"%Y年%M月%d日 %H时%i分%s秒");
+------------------------------------------------+
| date_format(now(),"%Y年%M月%d日 %H时%i分%s秒") |
+------------------------------------------------+
| 2017年April月29日 20时17分05秒                 |
+------------------------------------------------+
1 row in set (0.00 sec)

mysql> select date_format(now(),"%Y年%M月%d日 %H时%i分%s秒");
+------------------------------------------------+
| date_format(now(),"%Y年%M月%d日 %H时%i分%s秒") |
+------------------------------------------------+
| 2017年April月29日 20时17分10秒                 |
+------------------------------------------------+
1 row in set (0.00 sec)

(2)datediff()# 时间1减去时间2

select datediff(now(),"2016/4/29");


mysql> select datediff(now(),"2016/4/29");
+-----------------------------+
| datediff(now(),"2016/4/29") |
+-----------------------------+
|                         365 |
+-----------------------------+
1 row in set (0.10 sec)

(3)date_add()#增加一点时间
(4)date_sub()

mysql> select date_sub(now(),interval 1 hour);
+———————————+
| date_sub(now(),interval 1 hour) |
+———————————+
| 2017-04-29 19:25:20 |
+———————————+
1 row in set (0.03 sec)

(5)str_to_date()
把字符串类型转化为时间类型

select str_to_date("2015年3月4日","%Y年%m月%d日");
mysql> select str_to_date("2015年3月4日","%Y年%m月%d日");
+--------------------------------------------+
| str_to_date("2015年3月4日","%Y年%m月%d日") |
+--------------------------------------------+
| 2015-03-04                                 |
+--------------------------------------------+
1 row in set (0.00 sec)

牧牛

遛马

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值