MySQL之內置函數(日期函數、字符串函數、數學函數、加密函數等)

1.日期函數

函數名稱 描述
current_date() 當前日期
current_time() 當前時間
current_timestamp 當前時間戳
date(datetime) 返回datetime參數的日期部分
date_add(date,interval d_value_type) 在date中添加日期或時間,interval後的數值單位可以是:year, minute,second,day
date_sub(date,interval d_value_type) 在date中減去日期或時間,interval後的數值單位可以是:year, minute,second,day
datediff(date1,date2) 兩個日期的差,單位是天
now() 當前日期時間

範例:
1.1返回系統當前時間

mysql> select now(),curdate(),current_time();
+---------------------+------------+----------------+
| now()               | curdate()  | current_time() |
+---------------------+------------+----------------+
| 2019-07-25 00:22:35 | 2019-07-25 | 00:22:35       |
+---------------------+------------+----------------+

mysql> select current_date(),current_time(),current_timestamp();
+----------------+----------------+---------------------+
| current_date() | current_time() | current_timestamp() |
+----------------+----------------+---------------------+
| 2019-07-25     | 00:23:52       | 2019-07-25 00:23:52 |
+----------------+----------------+---------------------+

1.2在日期的基礎上加日期:

mysql> select date_add('2019-07-25',interval 10 day);
+----------------------------------------+
| date_add('2019-07-25',interval 10 day) |
+----------------------------------------+
| 2019-08-04                             |
+----------------------------------------+

1.3在日期的基礎上減去時間

mysql> select date_sub('2019-07-25',interval 10 day);
+----------------------------------------+
| date_sub('2019-07-25',interval 10 day) |
+----------------------------------------+
| 2019-07-15                             |
+----------------------------------------+

1.4計算兩個日期之間相差多少天

mysql> select datediff('2019-07-25','2018-06-12');
+-------------------------------------+
| datediff('2019-07-25','2018-06-12') |
+-------------------------------------+
|                                 408 |
+-------------------------------------+

1.5創建一張表,用來記錄生日

mysql> create table bir_table(
    -> id int primary key auto_increment,
    -> birthday date);

//添加當前日期
mysql> insert into bir_table(birthday) value(current_date());

mysql> select * from bir_table;
+----+------------+
| id | birthday   |
+----+------------+
|  1 | 2019-07-25 |
+----+------------+

1.6創建一個留言表

mysql> create table msg(
    -> id int primary key auto_increment,
    -> content varchar(30) not null,
    -> sendtime datetime
    -> );

//用戶添加留言
mysql> insert into msg(content,sendtime) values('晚上好', now());
mysql> insert into msg(content,sendtime) values('晚安,好夢', now());

//查看所有留言記錄
mysql> select * from msg;
+----+-----------------+---------------------+
| id | content         | sendtime            |
+----+-----------------+---------------------+
|  1 | 晚上好          | 2019-07-25 00:35:01 |
|  2 | 晚安,好夢      | 2019-07-25 00:35:23 |
+----+-----------------+---------------------+

顯示所有留言信息,發佈日期只顯示日期,不顯示時間

mysql> select content,date(sendtime) from msg;
+-----------------+----------------+
| content         | date(sendtime) |
+-----------------+----------------+
| 晚上好          | 2019-07-25     |
| 晚安,好夢      | 2019-07-25     |
+-----------------+----------------+

查詢在10分鐘內發佈的留言

mysql> select * from msg where date_add(sendtime,interval 10 minute)>now();
+----+-----------------+---------------------+
| id | content         | sendtime            |
+----+-----------------+---------------------+
|  1 | 晚上好          | 2019-07-25 00:35:01 |
|  2 | 晚安,好夢      | 2019-07-25 00:35:23 |
+----+-----------------+---------------------+

2.字符串函數

案例:

  • 獲取emp表的ename列的字符集
select charset(ename) from emp;
  • 要求顯示exam_result表中的信息,顯示格式:“XXX的語文是XXX分,數學XXX分,英語XXX分”
select concat(name, '的語文是',yuwen,'分,數學是',shuxue,'分') as '分數' from exam_result;
  • 將EMP表中所有名字中含有s的替換成’上海’
select replace(ename, 'S', '上海') ,ename from EMP;
  • 截取EMP表中ename字段的第二個到第三個字符
select substring(ename, 2, 2), ename from EMP;

3.數學函數

  • 絕對值
mysql> select abs(-100.2);
+-------------+
| abs(-100.2) |
+-------------+
|       100.2 |
+-------------+
  • 向上取整
mysql> select ceiling(23.04);
+----------------+
| ceiling(23.04) |
+----------------+
|             24 |
+----------------+
  • 保留2位小數位數(小數四捨五入)
mysql> select floor(23.7);
+-------------+
| floor(23.7) |
+-------------+
|          23 |
+-------------+
  • 產生隨機數
mysql> select rand();
+------------------+
| rand()           |
+------------------+
| 0.75725203357381 |
+------------------+
  • 十進制轉換二進制
mysql> select bin(10);
+---------+
| bin(10) |
+---------+
| 1010    |
+---------+

4.其它函數

  • user()查詢當前用戶
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
  • md5(str)對一個字符串進行md5摘要,摘要後得到一個32位字符串
mysql> select md5('123456');
+----------------------------------+
| md5('123456')                    |
+----------------------------------+
| e10adc3949ba59abbe56e057f20f883e |
+----------------------------------+
  • database()顯示當前正在使用的數據庫
mysql> select database();
+------------+
| database() |
+------------+
| test1      |
+------------+
  • password()函數,MySQL數據庫使用該函數對用戶加密
mysql> select password('root');
+-------------------------------------------+
| password('root')                          |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+
  • ifnull(val1,val2)如果val1爲null,返回val2,否則返回val1的值
mysql> select ifnull('abc','123');
+---------------------+
| ifnull('abc','123') |
+---------------------+
| abc                 |
+---------------------+
1 row in set (0.00 sec)

mysql> select ifnull(null,'123');
+--------------------+
| ifnull(null,'123') |
+--------------------+
| 123                |
+--------------------+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章