`
LJ你是唯一LT
  • 浏览: 238696 次
社区版块
存档分类
最新评论

MySQL常用函数

阅读更多
一、字符串类
1、left(str, length) 从左开始截取字符串    /right(str, length) 从右开始
说明:left(被截取字段,截取长度)
例:select left(title,5) from articles;    
       select right("abcdefghi",3);        ---ghi

2、ltrim(str)    去掉左侧的空格     /rtrim  去掉右侧的空格      /trim   去掉两侧的空格
例:select ltrim("   abc de fghi   ");     ---“abc de fghi   ” 
       select rtrim("   abc de fghi   ");    ---“   abc de fghi”
       select trim("   abc de fghi   ");     ---“abc de fghi”

3、substring(str, pos, [length]) 截取字符串
说明:substring(被截取字段,从第几位开始截取)
例:select substring(title,5) from articles;              ---从第5个字符开始取后面的所有
      select substring(title,5,10) from articles;          ---从第5个字符开始截取10个字符
      select substring(title,-2) from articles;              ---从倒数第2个字符开始截取到最后

4、substring_index(str,delim,count)按关键字截取字符串
说明:substring_index(被截取字段,关键字,关键字出现的次数)
例:select substring_index ("www.baidu.com",".",2);                         ---www.baidu
       select substring_index(`msg`,"/",5) from update_log where action=1;
(注:如果关键字出现的次数是负数 如-2 则是从后倒数,到字符串结束)

5、concat(str1,str2....strn)  字符串连接
例:select concat ("www.baidu.com",".","cn");      ---www.baidu.com.cn
       select concat (title,"-",body) from articles;

6、concat_ws(sep,str1,str2.....strn)  字符串连接,并用sep分隔开
例:select concat_ws (";","abc","def");         ---abc;def

7、insert(str1,x,y,str2)   将字符串str1从第x位置开始,y个字符长的子串替换为字符串str2,返回结果
例:select insert (title,2,3,"aaa") from articles;      ---MaaaL

8、lower(str)  将字符串全部换成小写     /upper(str)  将字符串全部换成大写
例:select lower("abCDefG");      ---abcdefg
        select upper("abCDefG")     ---ABCDEFG

9、length(str)  字符串的长度
例:select length("abCDefG");     --7

10、position(substr  in str)    返回子串substr在字符串str中第一次出现的位置
例:select position("C" in "abCDefG");      --3
二、数字类
1、abs(x)                    返回x的绝对值
例:select abs(-123);      ---123

2、ceiling(x)                返回大于x的最小整数值
例:select ceiling(-123.456);        -123
      select ceiling(123.456);          124

3、floor(x)            返回小于x的最大整数值
例:select floor(123.456);    123
       select floor(-123.456);   -124

4、greatest(x1,x2,...,xn)    返回集合中最大的值        /least(x1,x2,...,xn)       返回集合中最小的值
例:select greatest(1,10,100,-5);   100
      select  least(1,10,100,-5)      -5

5、mod(x,y)                  返回x/y的模(余数)
例:select mod(100,3);          1

6、rand()                     返回0到1内的随机值
例:select rand();         0.3541006747205748

7、round(x,y)                返回参数x的四舍五入的有y位小数的值
例:select round(123.456,2);     123.46

8、sqrt(x)        返回一个数的平方根
例:select sqrt(144);      12
三、日期时间类
1、curdate()或current_date()      返回当前的日期
例:select curdate();             2015-02-04

2、curtime()或current_time()        返回当前的时间             now()  当前的日期和时间
例:select curtime();          17:15:22
       select  now();          2015-02-04  17:16:01

3、date_add(date,interval int keyword)        返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化)
例:select  date_add(current_date,interval 6 month);    2015-08-04

4、date_format(date,fmt)       依照指定的fmt格式格式化日期date值
例: select  date_format(now(),'%m-%d-%Y %H:%i:%s  %a')                02-04-2015 17:21:41  Wed

5、date_sub(date,interval int keyword)        返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化)
例:select  date_sub(current_date,interval 6 month);

6、dayofweek(date)      返回date所代表的一星期中的第几天(1~7)
例:select  dayofweek(now());      4    今天是星期三
注意:1=星期天,2=星期一, ……7=星期六

7、dayofmonth(date)   返回date是一个月的第几天(1~31)     /dayofyear(date)    返回date是一年的第几天(1~366)
例:select  dayofmonth(now());     4
       select  dayofyear(now());      35

8、dayname(date)    返回date的星期名                        /month(date)    返回date的月份名
例: select   dayname(current_date);        Wednesday
        select  monthname(current_date);      February

9、hour(time)    返回time的小时值(0~23)           
/minute(time)    返回time的分钟值(0~59)           
/month(date)    返回date的月份值(1~12)
/quarter(date)    返回date在一年中的季度(1~4)
/week(date)       返回日期date为一年中第几周(0~53)
/year(date)        返回日期date的年份(1000~9999)
例:select   hour(current_time);    17
       select   quarter(current_date);    1


10、extract(unit FROM date)      返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
unit 常用的有:microsecond   second  minute  day  week  month  quarter year day_minute  hour_minute  year_month
例:select   extract(year_month from now());    201502
       select   extract(minute from now());        41

四、其他
1、cast(value as type)    类型转换函数       /convert(value,type)
说明:它可以把一个值转化为指定的数据类型。类型有
二进制,同带binary前缀的效果 : binary
字符型,可带参数 : char()
日期 : date
时间: time    
日期时间型 : datetime  
浮点数 : decimal
整数 : signed  
无符号整数 : unsigned
例:select cast('3.35' as signed);    3

2、系统信息函数
database()    返回当前数据库名
benchmark(count,expr)   将表达式expr重复运行count次
connection_id()    返回当前客户的连接ID
found_rows()    返回最后一个select查询进行检索的总行数
user()或system_user()   返回当前登陆用户名                                     
version()    返回MySQL服务器的版本
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics