mysql学习笔记五(常用函数)

1日期函数

 mysql学习笔记五(常用函数)

mysql> select current_date() from dual;

mysql> select current_time() from dual;

mysql> select month(current_date()) from dual;

mysql> select year(current_date()) from dual;

mysql> select month(current_date()) from dual;

mysql> select day(current_date()) from dual;

mysql> select hour(current_time()) from dual;

mysql> select minute(current_time()) from dual;

mysql> select second(current_time()) from dual;

 

create table message (

id int,

title varchar(64),

publishdate datetime

);

mysql> insert into message (id,title,publishdate)values(1,'abc',now());

Query OK, 1 row affected (0.09 sec)

 

mysql> insert into message (id,title,publishdate)values(1,'efc','2011-1-1');

Query OK, 1 row affected (0.09 sec)

备注:插入时间的内容可以是字符串,但是必须是日期格式

 

查询最近两个小时的内容

select * from message where date_add(publishdate,interval 2 hour)>= now();

 

选择了最近的 30天内所有带有date_col 值的记录: 

mysql> SELECT something FROM tbl_name

    -> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;

 

函数调用格式

DATE_ADD(date,INTERVAL expr type

DATE_SUB(date,INTERVAL expr type

 

type

预期的 expr格式

MICROSECOND

MICROSECONDS

SECOND

SECONDS

MINUTE

MINUTES

HOUR

HOURS

DAY

DAYS

WEEK

WEEKS

MONTH

MONTHS

QUARTER

QUARTERS

YEAR

YEARS

 

2字符串函数

 

mysql学习笔记五(常用函数)

 

mysql> select * from employee;

+------+------+------+------------+--------+------------+------------+

| id   | name | sex  | birthday   | salary | entry_date | resume     |

+------+------+------+------------+--------+------------+------------+

|    1 | aaa  |      | 1977-11-11 |   56.8 | 2014-11-10 | hello word |

|    2 | bbb  |      | 1977-11-11 |   57.8 | 2014-11-10 | hello word |

|    3 | ccc  |      | 1977-11-11 |   56.3 | 2014-11-10 | hello word |

+------+------+------+------------+--------+------------+------------+

1rows in set (0.00 sec)

 

名字首字母大写

select SUBSTRING(UCASE(name),1,1) from employee;

select SUBSTRING(LCASE(name),2,LENGTH(name)-1) from employee;

select CONCAT(SUBSTRING(UCASE(name),1,1), SUBSTRING(LCASE(name),2,LENGTH(name)-1)) from employee;

 

备注:SUBSTRING从第一个数从0开始

 

3数学函数

mysql学习笔记五(常用函数)

 

4加密函数

4.1md5加密

 

mysql> select md5('huangbiao') from dual;

+----------------------------------+

| md5('huangbiao')                 |

+----------------------------------+

| 1d7877a910293815774015e3776afa0c |

+----------------------------------+

1 row in set (0.00 sec)

 

mysql> select md5('huangbiao') ;

+----------------------------------+

| md5('huangbiao')                 |

+----------------------------------+

| 1d7877a910293815774015e3776afa0c |

+----------------------------------+

 

4.2password(str)

从原文密码str 计算并返回密码字符串,当参数为 NULL 时返回 NULL。这个函数用于用户授权表的Password列中的加密MySQL密码存储

 

mysql> select password('1');

+-------------------------------------------+

| password('1')                             |

+-------------------------------------------+

| *E6CC90B878B948C35E92B003C792C46C58C4AF40 |

+-------------------------------------------+ 

 

mysql> select password(NULL);

+----------------+

| password(NULL) |

+----------------+

| NULL           |

+----------------+

 

4.3SHA1(str) SHA(str) 

 

mysql> SELECT SHA1('1');

+------------------------------------------+

| SHA1('1')                                |

+------------------------------------------+

| 356a192b7913b04c54574d18c28d46e6395428ab |

+------------------------------------------+

1 row in set (0.00 sec)

 

mysql> SELECT SHA('1');

+------------------------------------------+

| SHA('1')                                 |

+------------------------------------------+

| 356a192b7913b04c54574d18c28d46e6395428ab |

+------------------------------------------+

1 row in set (0.00 sec)

 

 

4.4DECODE(crypt_str,pass_str)  和 ENCODE(str,pass_str)

 

使用 pass_str 作为密码,解密加密字符串 crypt_str, crypt_str 应该是由ENCODE()返回的字符串。 

使用pass_str 作为密码,解密 str 。 使用DECODE()解密结果

 

你可能感兴趣的:(mysql)