Mysql常用函数

 

mysql函数一般分为:数学函数,字符串函数,日期和时间函数,条件判断函数,系统信息函数,加密函数,其他函数;本次暂时只是列举出最常用的查询函数,和部分不需要涉及表查询的函数


1、数学函数中的随机函数

mysql> SELECT RAND();
+-------------------+
| RAND() |
+-------------------+
| 0.465400918820315 |
+-------------------+
1 row in set (0.03 sec)
mysql> SELECT RAND()*100;
+------------------+
| RAND()*100 |
+------------------+
| 44.7935999788284 |
+------------------+
1 row in set (0.01 sec)

 

2、字符串函数,计算字符串字符数和字符串长度

mysql> select user,CHAR_LENGTH(user),LENGTH(user) from mysql.user where user='backup'; 
+--------+-------------------+--------------+
| user | CHAR_LENGTH(user) | LENGTH(user) |
+--------+-------------------+--------------+
| backup | 6 | 6 |
+--------+-------------------+--------------+
1 row in set (0.00 sec)

 

3、日期函数

下面依次是:当前日期,当前时间,当前日期和时间

mysql> SELECT CURRENT_DATE(),CURRENT_TIME(),NOW();
+----------------+----------------+---------------------+
| CURRENT_DATE() | CURRENT_TIME() | NOW() |
+----------------+----------------+---------------------+
| 2016-02-18 | 06:25:52 | 2016-02-18 06:25:52 |
+----------------+----------------+---------------------+
1 row in set (0.00 sec)

 

下面依次是:当前日期和时间,UNIX时间戳,UTC日期,UTC时间

mysql> SELECT NOW(),UNIX_TIMESTAMP(),UTC_DATE(),UTC_TIME;
+---------------------+------------------+------------+----------+
| NOW() | UNIX_TIMESTAMP() | UTC_DATE() | UTC_TIME |
+---------------------+------------------+------------+----------+
| 2016-02-18 06:27:56 | 1455748076 | 2016-02-17 | 22:27:56 |
+---------------------+------------------+------------+----------+

 


4、系统信息函数
下面依次是:数据库版本号,服务器连接数,数据库名称,当前用户,返回最后生成的 AUTO_INCREMENT 值

mysql> SELECT VERSION(),CONNECTION_ID(),DATABASE(),USER(),CURRENT_USER(),LAST_INSERT_ID(); 
+------------+-----------------+------------+----------+----------------+----------------+------------------+
| VERSION() | CONNECTION_ID() | DATABASE() | USER() | CURRENT_USER() | LAST_INSERT_ID() |
+------------+-----------------+------------+----------+----------------+----------------+------------------+
| 5.1.73-log | 17 | zjf | root@localhost | root@localhost | 0 |
+------------+-----------------+------------+----------+----------------+----------------+------------------+
1 row in set (0.00 sec)

 

5、加密函数

用户密码加密PASSWORD

mysql> SELECT PASSWORD('123');
+-------------------------------------------+
| PASSWORD('123') |
+-------------------------------------------+
| *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-------------------------------------------+
1 row in set (0.00 sec)

 

普通数据加密MD5

mysql> SELECT MD5('123');
+----------------------------------+
| MD5('123') |
+----------------------------------+
| 202cb962ac59075b964b07152d234b70 |
+----------------------------------+
1 row in set (0.00 sec)

 

你可能感兴趣的:(Mysql常用函数)