mysql中常用的函数

来源:互联网 发布:淘宝怎么写评论 编辑:程序博客网 时间:2024/05/16 15:36

总结一下,自己经常会使用到的mysql 函数,与大家分享

1)字符串连接函数 concat(str1,str2,str2...)

2)提取子串函数 substring(str,start,len)

  其中,若省略len 则是从start开始到字串结束

  若省略len,且start为负数,则是从字符串末尾往前提取|start|个字符

select c.id,c.fullname,u.id uid, concat(u.lastname,u.firstname) as author,           u.department as dept,c.summary,ct.id as fid,f.filenamefrom mdl_course cleft join mdl_log lgon substring(lg.url,13) = c.id and lg.action = 'new'left join mdl_user uon lg.userid = u.idleft join mdl_context cton ct.instanceid = c.id and ct.contextlevel = 50left join mdl_files fon f.contextid = ct.id and f.filename !='.'ORDER BY c.timemodified desc limit 8";
3)去重函数 distinct(field)

select distinct(name) from usergrade

4)求和函数 sum(field)

   使用求和函数时,一般会使用group by 进行分组

select sum(grade) ,username from usergrade group by classid

5)统计函数 count(field) 这个太常见,一般使用PHP自带count就可以实现


6)生成md5 加密函数 md5(str)

   

select md5('123456')

暂时就写这么多,用到了再加上来。

2014-09-28 补充函数:

7)连接查询结果函数 group_concat(field) 默认以','分割

8)正则表达式过滤函数 regexp '正则表达式'

   这个非常强大,要了解正则相关知识 

select group_concat(id) ids from mdl_course_categories where path regexp '/3/' or id=3
9)查询子串是否存在 INSTR(str,substr)

   或者substring_index(str,substr, offset) 两者都非常强大

   在无限级分类的path路径寻找中非常有用

 

select c.id, c.fullname, c.startdate, substr(substring_index(path, '/', 2) , 2) as topcateidfrom mdl_course cleft join mdl_course_categories cc on c.category=cc.id

















0 0
原创粉丝点击