mysql中常用的函数

总结一下,自己经常会使用到的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.filename
				from mdl_course c
				left join mdl_log lg
				on substring(lg.url,13) = c.id and lg.action = 'new'
				left join mdl_user u
				on lg.userid = u.id
				left join mdl_context ct
				on ct.instanceid = c.id and ct.contextlevel = 50
				left join mdl_files f
				on 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 topcateid
from mdl_course c
left join mdl_course_categories cc on c.category=cc.id

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值