当前位置:   article > 正文

Mysql基础查询学习_现在你需要查看前2个用户明细设备id数据,并将列名改为 'user_infos_example',,

现在你需要查看前2个用户明细设备id数据,并将列名改为 'user_infos_example',,请

以user_profile表为例

1.现在运营想要查看用户信息表中所有的数据,请你取出相应结果(user_profile表)
select * from user_profile

2.现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据(user_profile表)
select device_id,gender,age,university from user_profile

3.现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。(user_profile表)
select distinct university from user_profile

4.现在运营只需要查看前2个用户明细设备ID数据,请你从用户信息表 user_profile 中取出相应结果。(user_profile表)
select  device_id from user_profile LIMIT 2

5.现在你需要查看前2个用户明细设备ID数据,并将列名改为 'user_infos_example',,请你从用户信息表取出相应结果。(user_profile表)
select device_id AS user_infos_example from user_profile Limit 2


6.现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。
select device_id,university from user_profile where university = '北京大学' and device_id = user_profile.device_id  (device_id = user_profile.device_id 100%的索引覆盖,不用回表查询)

7.现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。
select device_id,gender,age,university from user_profile where age is not null and age>24   (严谨起见,加上age is not null)

8.现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。
1)使用and连接,select device_id,gender,age from user_profile where age >=20 and age <=23
2)使用between ...and连接,select device_id,gender,age from user_profile where age between 20 and 23

9.现在运营想要查看除复旦大学以外的所有用户明细,请你取出设备ID、性别、年龄、学校相应数据。
select device_id,gender,age,university from user_profile where university != '复旦大学'
select device_id,gender,age,university from user_profile where university not in('复旦大学') 

10.用where过滤空值练习:现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息
select device_id,gender,age,university from user_profile where age is not null
select device_id,gender,age,university from user_profile where age !=''
 

11.现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。
select device_id,gender,age,university,gpa from user_profile where gender = 'male' and gpa >3.5

12.现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据(使用OR实现)
select device_id,gender,age,university,gpa from user_profile where university ='北京大学' or gpa >3.7

13.(where in 和Not in)现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。
1) select device_id,gender,age,university,gpa from user_profile where university  in ("北京大学","复旦大学","山东大学")
2) select device_id,gender,age,university,gpa from user_profile where university not in ("浙江大学")

14.现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据
select device_id,gender,age,university,gpa from user_profile where (gpa >3.5 and university = '山东大学' ) or (gpa >3.8 and university = '复旦大学')

15.现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。
select device_id,age,university from user_profile where university like '%北京%'

5.运营想要知道复旦大学学生gpa最高值是多少,请你取出相应数据
select max(gpa) as gpa from user_profile where university='复旦大学'
select  gpa from user_profile where university='复旦大学' order by gpa DESC limit 1  ——降序排序,只输出第一个

16.现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。
select count(gender) as male_num ,round(avg(gpa),1) as avg_gpa from user_profile where gender = 'male'
count(列名称)计算总数函数,round 返回一个数值,并指定数的长度。avg(列名称) 计算平均数的函数

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/438015
推荐阅读
相关标签
  

闽ICP备14008679号