# todo 2022/11/30 15:57
"""
mysql -u root -p
123456
show databases;
use sa3mall;
show tables;
select * from course; 升序
select * from course order by tno; 升序
select * from course order by tno asc; 升序
select * from course order by tno desc,cno desc; 多字段排序
select * from course order by 3 asc; 位置排序
select ssex,count(*) from student group by ssex; 统计性别的数量
group by 分组
with rollup 统计 count avg sum 统计总数
select coalesce(ssex,'总数'),sum(class) as Class from student group by ssex with rollup;
* 多表连查 inner join,连接两个数据表
select * from score inner join student on score.sno = student.sno;
* 连接三个表
select * from (score inner join student on score.sno = student.sno)inner join course on score.cno=course.cno;
"""
"""
2022/11/30 13:26
创建多级目录 mkdir -p yuwen/test
查询所有列 select * from user_profile;
查询多列 select device_id,gender,age,university from user_profile;
查询一列去重 select distinct university from user_profile;
读取某列,前两个字数 select device_id from user_profile limit 0,2;
将查询后的列表重新命名 select device_id as user_infos_example from user_profile limit 2;
查询后对年龄升序 select device_id,age from user_profile order by age asc;
查找后多列升序 select device_id,gpa,age from user_profile order by gpa,age;
查找后多列降序 select device_id,gpa,age from user_profile order by gpa desc,age desc;
查找学校是北大的学生信息 select device_id,university from user_profile where university='北京大学';
查找年龄大于24岁的用户信息 select device_id,gender,age,university from user_profile where age >24;
查找某个年龄段的用户信息 select device_id,gender,age from user_profile where age >=20 and age<=23;
插入为空 null
查找除复旦大学的用户信息 select device_id,gender,age,university from user_profile where university !='复旦大学';
用where过滤不是空值练习 select device_id,gender,age,university from user_profile where age is not null;
现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。
select device_id,gender,age,university,gpa from user_profile where gpa > 3.5 and gender='male';
现在运营想要找到学校为北大或GPA在3.7以上的用户进行调研,请你取出相关数据
select device_id,gender,age,university,gpa from user_profile where gpa > 3.7 or university='北京大学';
现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。
select device_id,gender,age,university,gpa from user_profile where university in("北京大学","复旦大学","山东大学");
现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据
select device_id,gender,age,university,gpa from user_profile
where university='山东大学' and gpa>3.5 or university='复旦大学' and gpa>3.8;
查看学校名称中含北京的用户
select device_id,age,university from user_profile where university like "%北京%";
运营想要知道复旦大学学生gpa最高值是多少,请你取出相应数据
select max(gpa) from user_profile where university='复旦大学';
现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。
select count(gender) as male_num,round(avg(gpa),1) as avg_gpa from user_profile where gender='male';
现在运营想要对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。
性别 学校 设备id 活跃天数 发贴 回复 每(分组) 学校(分组)
select gender,university, count(device_id), avg(active_days_within_30),avg(question_cnt) FROM user_profile group by gender,university;
现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,
请取出平均发贴数低于5的学校或平均回帖数小于20的学校。
having 有的意思
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;
"""2022