运维实习
#牛客创作赏金赛##记录你的求职/职场日常#
最常用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'
最常用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'
全部评论
相关推荐
10-30 17:15
北京交通大学 Python 点赞 评论 收藏
分享
点赞 评论 收藏
分享