牛客网mysql刷题记录
Posted 程序媛一枚~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了牛客网mysql刷题记录相关的知识,希望对你有一定的参考价值。
牛客网mysql刷题记录
- SQL26 计算25岁以上和以下的用户数量
if(expression,A,B)
select if(age < 25 or age is null, '25岁以下','25岁及以上') as age_cut,
count(u.device_id)
from user_profile u group by age_cut;
select device_id,gender,
(case when age is null then '其他'
when age<20 then '20岁一下'
when age<24 then '20-24岁'
else '25岁及以上' end) as age_cut from user_profile;
当有字段名和系统字段名重名时,需要表上加别名。
select substr(q.date,9,2) as day,count(substr(q.date,1,7)) as question_cnt from question_practice_detail q
where q.date like '%-08-%'
group by substr(q.date,9,2)
- SQL29 计算用户的平均次日留存率
MySQL中 COUNT在对列进行计数时不统计值为 null的条目
有重复需去重
select
count(distinct q2.device_id,q2.date)/count(distinct q1.device_id,q1.date) as avg_ret
from question_practice_detail q1 left join question_practice_detail q2
on q1.device_id=q2.device_id and datediff(q2.date,q1.date) = 1;
SELECT
COUNT(q2.device_id) / COUNT(q1.device_id) AS avg_ret
FROM
(SELECT DISTINCT device_id, date FROM question_practice_detail)as q1
LEFT JOIN
(SELECT DISTINCT device_id, date FROM question_practice_detail) AS q2
ON q1.device_id = q2.device_id AND q2.date = DATE_ADD(q1.date, interval 1 day)
从字段中截取值后匹配
SELECT IF(profile LIKE '%female%','female','male') gender,COUNT(*) number
FROM user_submit
GROUP BY gender;
select device_id,university,gpa from user_profile where (university,gpa) in (select university,min(gpa) from user_profile group by university) order by university
SELECT device_id,university,gpa FROM
(SELECT device_id,university,gpa,
RANK() over (PARTITION BY university ORDER BY gpa) rk FROM user_profile) a
WHERE a.rk=1;
2. 用到的函数
if(expression, A,B)
case when else end
substr(str,i)
substr(str,i,j)
固定字符串的截取 substring_index(blog_url,‘/’,-1) http:/ur/tiger 提取出tiger
以上是关于牛客网mysql刷题记录的主要内容,如果未能解决你的问题,请参考以下文章