运维实习

#牛客创作赏金赛##记录你的求职/职场日常#
最常用SQL Server和Oracle

MySQL中字符串截取主要包括三种截取方式,一是截取字符串前(后)几个字符,二是按照长度截取,三是按照分隔符截取。

一、截取方式

类型函数名描述
截取前(后)几个字符left(str,n)返回字符串 str 的前 n 个字符
right(str,n)返回字符串 str 的后 n 个字符
按长度截取mid(str, start, length)从字符串 str 的 start 位置开始截取长度为 length 的子字符串
substr(str, start, length)从字符串 str 的 start 位置开始截取长度为 length 的子字符串
substring(str, start, length)从字符串 str 的 start 位置开始截取长度为 length 的子字符串
按分隔符截取substring_index(str, delimiter, number)返回从字符串 s 的第 number 个出现的分隔符 delimiter 之后的子串。
如果 number 是正数,返回第 number 个字符左边的字符串。
如果 number 是负数,返回第(number 的绝对值(从右边数))个字符右边的字符串

二、实例
select
#返回字符串 student 中的前3个字符:
left('student',3), #stu
#返回字符串 student 的后两个字符:
right('student',3),#ent
#从字符串 'student' 中的第 2 个位置开始截取 3个 字符:
mid('student', 2, 3), #tud 
substr('student', 2, 3), #tud
substring('student', 2, 3), #tud 
#如果 number 是正数,返回正数第 number 个分隔符左边的字符串
substring_index('student,学生,12',',',1), #student
substring_index('student,学生,12',',',2), #student,学生
#如果 number 是负数,返回倒数第(绝对值number)个分隔符右边的字符串。
substring_index('student,学生,12',',',-1),  #12
substring_index('student,学生,12',',',-2),  #学生,12
substring_index(substring_index('student,学生,12',',',-2),',',1)  #学生

#输出结果:stu|end|tud|tud|tud|student|student,学生|12|学生,12|学生

求解代码
方法一: month和year

select
    count(distinct device_id) as did_cnt,
    count(device_id) as question_cnt
from question_practice_detail
where month(date) = 8 and year(date) = 2021

方法二: date_format

select
    count(distinct device_id) as did_cnt,
    count(device_id) as question_cnt
from question_practice_detail
where date_format(date,'%Y%m') = '202108'

方法三: date_format

select
    count(distinct device_id) as did_cnt,
    count(device_id) as question_cnt
from question_practice_detail
where date_format(date,'%y%m') = '2108' 

方法四: like

select
    count(distinct device_id) as did_cnt,
    count(device_id) as question_cnt
from question_practice_detail
where date like '2021-08%' 

方法五: substring

select
    count(distinct device_id) as did_cnt,
    count(device_id) as question_cnt
from question_practice_detail
where substring(date,1,7) = '2021-08' 

方法六: mid

select
    count(distinct device_id) as did_cnt,
    count(device_id) as question_cnt
from question_practice_detail
where mid(date,1,7) = '2021-08'

方法七: left

select
    count(distinct device_id) as did_cnt,
    count(device_id) as question_cnt
from question_practice_detail
where left(date,7) = '2021-08'

方法八: substring_index

select
    count(distinct device_id) as did_cnt,
    count(device_id) as question_cnt
from question_practice_detail
where substring_index(date,'-',2) = '2021-08'
全部评论

相关推荐

03-25 19:43
湖北大学 C++
点赞 评论 收藏
分享
03-18 01:22
门头沟学院 Java
多多爱我我爱多多:linkedList 替换 arrayList 是怎么实现20倍提升的 好奇
点赞 评论 收藏
分享
评论
2
3
分享

创作者周榜

更多
正在热议
更多
# 一张图晒出你司的标语 #
4402次浏览 77人参与
# 找AI工作可以去哪些公司? #
9571次浏览 260人参与
# 米连集团26产品管培生项目 #
13431次浏览 285人参与
# 你的实习产出是真实的还是包装的? #
20514次浏览 343人参与
# AI面会问哪些问题? #
28498次浏览 578人参与
# 春招至今,你的战绩如何? #
66857次浏览 589人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
15496次浏览 224人参与
# 从事AI岗需要掌握哪些技术栈? #
9419次浏览 334人参与
# 中国电信笔试 #
32166次浏览 295人参与
# 你做过最难的笔试是哪家公司 #
34783次浏览 258人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
341064次浏览 2175人参与
# 金三银四,你的春招进行到哪个阶段了? #
22404次浏览 284人参与
# 同bg的你秋招战况如何? #
212254次浏览 1121人参与
# 哪些公司真双非友好? #
69762次浏览 289人参与
# 如何准备秋招 #
78321次浏览 868人参与
# 阿里笔试 #
179170次浏览 1318人参与
# 机械人避雷的岗位/公司 #
62716次浏览 393人参与
# 小马智行求职进展汇总 #
25149次浏览 80人参与
# 第一份工作一定要去大厂吗 #
15003次浏览 122人参与
# 担心入职之后被发现很菜怎么办 #
291410次浏览 1210人参与
# 为了减少AI幻觉,你注入过哪些设定? #
26298次浏览 310人参与
# 应届生第一份工资要多少合适 #
20709次浏览 86人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务