MySQL内置函数(日期,字符串,数学,其他)

一.日期函数

1.获得年月日

select current_date();

   

2.获得时分秒

select current_time();

  

3.获得时间戳

select current_timestamp;

   

select unix_timestamp;

  

4.返回datetime参数的日期部分

select date(datetime);

  

5.在日期的基础上加上日期,interval后的数值单位可以是year ,minute,second,day

select date_add(date,interval d_value_type);

  

6.在日期的基础上减去日期,interval后的数值单位可以是year ,minute,second,day

select date_sub(date,interval d_value_type);

  

7.计算两个日期之间相差多少天

select datediff(date1,date2);

   

二.字符串函数

1.返回msg表的id列的字符集

select charset(id) from msg;

  

2.要求显示student表中的信息,显示格式:“XXX的英语是XXX分,语文是XXX分”

select concat(string1[,]) from student;

   

3. 返回substring在string中出现的位置

select instr(string,substring);

  

4.转换成大写

select ucase(string);

  

5.转换成小写

select lcase(string);

  

6.从string中左起取length个长度

select left(string,length);

  

7.string的长度

select length(string);

  求学生表中学生姓名占用的字节数:

   

8.在str中用string2替换string1

select replace(str,string1,string2);

  在student表中name这一列,把Jack全部用Bob替换:

  

9.substring:从str的第poistion个位置开始,取length个字母

select substring(str,poistion,length);

在student表中name列,取name的前两个字母: 

   

10.综合:运用substring,concat,lcase

以首字母小写的方式显示所有学生的姓名:

三.数学函数

1.绝对值

2.向上取整

3.向下取整

4.保留两位小数(小数四舍五入)

5.产生随机数

 

四. 其他函数

1.user() 查询当前用户

  
2.md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串

  
3.database()显示当前正在使用的数据库

  
4.password()函数,MySQL数据库使用该函数对用户加密

select password ('root');

5.ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值

  

猜你喜欢

转载自blog.csdn.net/sophie1314/article/details/83714569