题解 | #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函数:
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函数
SUBSTRING函数是文本处理函数,可以截取字符串
格式: SUBSTRING(s, start, length)
从字符串s的start位置截取长度为length的子字符串
如果SUBSTRING()函数接收2个参数:SUBSTRING(s,start),则第一个参数为待截取的字符串,第二个参数为截取的起始位置。如果第二个参数为负整数,则为倒数的起始位置。

查看3道真题和解析