题解 39 | #21年8月份练题总数#

【场景】:某月的情况 【分类】:聚合函数、日期函数、字符串函数

分析思路

难点:

1.计算总用户数时要对用户去重

2021年8月可以怎么写

  • [使用]:month和year
  • [使用]:date_format
  • [使用]:like
  • [使用]:mid
  • [使用]:left
  • [使用]:substring_index

扩展

前往查看:MySQL 字符串截取 left、right、substring、substring_index

求解代码

方法一: 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' 
全部评论

相关推荐

程序员牛肉:可以说含金量不如王者荣耀省标。
点赞 评论 收藏
分享
评论
104
5
分享

创作者周榜

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