题解 | #substring_index函数#

计算用户8月每天的练题数量

https://www.nowcoder.com/practice/847373e2fe8d47b4a2c294bdb5bda8b6

select SUBSTRING_INDEX(question_practice_detail.date,'-',-1),count(question_id) AS question_cnt 
from question_practice_detail
where question_practice_detail.date like '2021-08%'
group by question_practice_detail.date;

这道题因为要将year-month-day只取day,所以用到了SUBSTRING_INDEX函数,用于将字符串进行截取。

补充:

substring_index函数:

http://t.csdnimg.cn/pdcRW

substring_index(string,sep,num)即substring_index(字符串,分隔符,序号)

参数说明

string:用于截取目标字符串的字符串。可为字段,表达式等。

sep:分隔符,string存在且用于分割的字符,比如“,”、“.”等。

num:序号,为非0整数。若为整数则表示从左到右数,若为负数则从右到左数。比如“www.mysql.com”截取字符‘www’,分割符为“.”,从左到右序号为1,即substring_index("www.mysql.com",'.',1);若从右开始获取“com”则为序号为-1即substring_index("www.mysql.com",'.',-1)

SUBSTRING函数

http://t.csdnimg.cn/QBR9R

SUBSTRING函数是文本处理函数,可以截取字符串

格式: SUBSTRING(s, start, length)

从字符串s的start位置截取长度为length的子字符串

如果SUBSTRING()函数接收2个参数:SUBSTRING(s,start),则第一个参数为待截取的字符串,第二个参数为截取的起始位置。如果第二个参数为负整数,则为倒数的起始位置。

全部评论

相关推荐

09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务