mysql-6

续上
mysql中常见函数

字符串函数
  1. cancat(s1,s2,…):把传入的参数连接成为一个字符串.
    select concat(‘aaa’,’bbbb’),concat(‘aaa’,null);
    上面代码将所输入的字符串拼接起来.

  2. insert(str,x,y,instr):将字符串str从第x位置开始,y个字符长的子串替换成字符串instr.
    select insert(‘beijing2008’,12,4,’me’)
    执行该命令,从12开始后的四个字符将会被替换成为me

  3. lower(str)和upper(str):将所给字符串,小写变成大写,大写变成小写.

  4. left(str,x)和right(str,x):上面两个函数将会返回字符串str最左边的x个字符以及最右边的x字符.
    select left(‘ctwgogoctwgogo’,6),right(‘ctwgogoctwgogo’,4);
    其结果将会是:
    ctwgog,gogo;

  5. lpad(str,n,pad)和rpad(str,n,pad):用字符串pad对str最左边和最右边n个字符进行填充,
    select lpad(‘ctw’,10,’str’),rpad(‘ctwg’,10,’str’);

  6. ltrim(str)和rtrim(str):去掉字符串str左侧和右侧空格.

  7. repeat(str,x):返回str重复x次的结果
  8. replace(str,a,b):用字符串b替换字符串str中所有出现的字符str.
    select replace(‘ctwgogo’,’gogo’,’ctw’);
  9. trim(str):去掉字符串str中的空格.
  10. substring(str,x,y):返回str中第x位置起y个字符长度的字符串
    select substring(‘ctwgogo’,4,3);
数值函数
  1. abs(x):返回x的绝对值
  2. ceil(x):返回大于x的最大整数.
    select ceil(-0.8),ceil(0.8);
    其结果为0和1;

  3. floor(x):返回小于x的最大整数
    select floor(0.8),floor(-0.8);
    其运行结果为0和-1.

  4. mod(x):对x求模

  5. rand():返回随机数,可以返回任意大小的随机数:其命令如下:
    select 100*rand(),ceil(100*rand());
  6. round(x,y):返回参数x的四舍五入的有y位小数的值.
  7. truncate(x,y):返回数字x截断为y位小数的结果.truncate()和round()的区别在于truncate()是直接截断.
日期和时间函数
  1. curdate():返回当前时间,只包含年月日
  2. curtime():返回当前时间,只包含时分秒
  3. now():返回当前时间和日期,年月日时分秒全部包含
  4. week(date)和year(date):前者返回所给日期是一年中的第几周,后者返回所给的日期是第几年
流程函数
  1. if(value,t,f):如果value是真,返回t,否则返回f.
    select if(salary>1,’high’,’low’);
    如果salary大于1时,表示高薪,小于1时,表示低薪

  2. ifnull(value1,value2):如果value1不为空,返回value1,否则返回values2,这个函数是用来替换null值,对null值进行处理.
    select ifnull(salary,0) from salary;

  3. case when [value1] then[result1]…else[default] end:
    select case when salary<=2000 then ‘low’ else ‘high’ end form emp;

猜你喜欢

转载自blog.csdn.net/wilder_ting/article/details/78937068