mysql数据库的查询操作
1.查询多列
select device_id,gender,age,university from user_profile
2.查询一列结果去重
-
加关键词 DISTINCT
SELECT DISTINCT university from user_profile
3.查询结果限制返回行数
-
使用limit语句解析: select device_id from user_profile limit 0,2 limit可接受1或者2个参数。接受1个参数时默认主键列/筛选列行数开始计数;接受2个参数时,第一个参数是行数序号(从0开始表示第一行),第二个参数是数量。如本题需筛选出前2行 LIMIT 0,2 从第0行 开始到第2行
select device_id from user_profile LIMIT 0,2
4.查询的列重新命名
-
加关键词 as user_infos_example
select device_id as user_infos_example from user_profile limit 0,2
5.查找学校里面是北大的学校
-
加关键词 where university=北京大学’
select device_id,university from user_profile where university=北京大学 limit 0,2
6.查询不包括某个字段的值
-
加关键词 NOT IN (“复旦大学”)
SELECT device_id, gender,age,university FROM user_profile WHERE university NOT IN ("复旦大学")
7.查询age这一列不为空的其他值(去空值)
-
加关键词 is not NULL
select device_id,gender,age,university from user_profile where age is not NULL
8.查询学校在这几个之内的(in not in)
-
加关键词 in (‘北京大学’,‘复旦大学’,‘山东大学’ )
select device_id,gender,age,university,gpa from user_profile where university in (北京大学,复旦大学,山东大学 )
9.查询学校内包含北京的所有学校
-
加关键词 ** like’北京%’** like’北京%’ 是 北京某某某 的意思
select device_id,age,university from user_profile where university like北京%
10.查找gpa最高值
-
加关键词 max(gpa) 函数在上面这里添加
select max(gpa) from user_profile
11.计算男生人数以及平均GPA
-
加关键词 count(gender),round(avg(gpa),1) 按照示例保存一位小数,用round函数,round函数用于把数值段舍入为指定的小数位
select count(gender),round(avg(gpa),1) from user_profile where gender=male
12.先分组然后对其求和求平均
-
生成表的代码
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);
-
加关键词 GROUP by gender,university 对 gender,university 来进行分组
select gender,university,count(gender)as user_num, avg(active_days_within_30) as avg_active_day, avg(question_cnt) as avg_question_cnt from user_profile GROUP by gender,university
13.分组
-
取出平均发贴数低于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
14.按照升序排列(asc)
-
加关键词 order by avg_question_cnt asc desc 按照降序排序
select university ,avg(question_cnt) as avg_question_cnt from user_profile group by university order by avg_question_cnt asc
15.多表查询
-
来个表里都有共用的 device_id 字段
select device_id,question_id,result from question_practice_detail where device_id in( select device_id from user_profile where university=浙江大学 )
下一篇:
Mysql如何快速插入千万条数据实战