前言
题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` float,
`answer_cnt` float
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
MySQL语法
代码如下:文章来源:https://www.toymoban.com/news/detail-855987.html
select university,AVG(question_cnt) as avg_question_cnt,AVG(answer_cnt) as avg_answer_cnt from user_profile
group by university having avg_question_cnt< 5 or avg_answer_cnt <20
注意:
- WHERE 关键字无法与合计函数一起使用;
- SQL语句执行顺序
FROM - ON - JOIN - WHERE - GROUP BY - avg,sum.... - HAVING - SELECT - DISTINCT - ORDER BY - LIMIT
(8) SELECT (9) DISTINCT
(1) FROM
(3) JOIN
(2) ON
(4) WHERE
(5) GROUP BY
(6) avg,sum…
(7) HAVING
(10) ORDER BY
(11) LIMIT文章来源地址https://www.toymoban.com/news/detail-855987.html
到了这里,关于【SQL每日一练】分组过滤练习题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!