02数据库查询
数据使用where子句
数据库中可以使用where子句进行搜索条件过滤
select * from table where 条件
查询出python成绩大于60分以上的学员信息
SELECT name,python_score FROM score WHERE python_score >=60;
where子句的操作符
SELECT name,python_score FROM score WHERE python_score=92
SELECT name,python_score FROM score WHERE python_score BETWEEN 90 AND 100
SELECT name,python_score FROM score WHERE python_score BETWEEN 90 AND 100 ORDER BY python_score DESC
检查空值
使用 is null 来检查空值
SELECT name,python_score from score WHERE python_score is NULL #过滤掉python成绩为空的信息
使用is not null来检查非空值
SELECT name,python_score from score WHERE python_score is NOT NULL #同样使用is not null来过滤非空
NOT表示相反
可以在where子句条件前边添加Not关键字,表示相反
SELECT name,python_score FROM score -- 不大于等于60 WHERE NOT python_score>=60;
SELECT name,python_score FROM score -- 不为空 WHERE NOT python_score is NULL;
AND并且操作符
可以给where子句添加多个条件进行过滤。比如:查询同时python成绩和sql成绩都大于60的学员信息
SELECT * from score WHERE python_score >=60 AND sql_score>=60
SELECT * from score WHERE python_score >=60 AND sql_score>=60
OR 或者操作符
or 表示或者关系,在多个条件从句只要符合其中的1个条件即可。
在学生成绩表中,过滤出python或者sql成绩 只要其中有1个为>=60即可。
SELECT * FROM score where python_score >= 60 OR -- 表示或者 sql_score >= 60
and 也可以和 or 运算在一起使用; 使用时可以使用 ()
来限定执行顺序;
比如:
查询 test_score >=60
并且 python_score 或者 sql_score 中只要有1个大于等于60 即可。
SELECT * FROM score WHERE test_score >= 60 AND -- 使用() 先把() 中结果运算出来,之后再跟上面做And运算 (python_score >= 60 OR sql_score >= 60 )
IN 操作符
in 表示在给定条件范围内的结果;
查询Python成绩为(80,90,100) 只要符合这三个中的1个即可。
SELECT * FROM score where python_score in (80,90,100)
模糊查询
Like 操作符
使用like可以进行关键字匹配,进行模糊插叙,比如查询一个班级中学员姓名包含 张
的同学
百分号(%)通配符
在搜索串中,% 表示任意字符出现任意次数
SELECT * from score WHERE -- 使用 '' 或者 "" 表示字符串 name LIKE '张%';
也可以多个条件匹配
SELECT * from score WHERE -- 使用 '' 或者 "" 表示字符串 name LIKE '张%' Or name like "王%" OR name LIKE "李%" OR name LIKE "赵%"
查询的为姓张或者姓王或者姓李或者姓赵的学员信息;
下划线(_) 通配符
_ 与 % 的区别: % 匹配所有长度字符串,
_ 只匹配一个字符
SELECT * from score WHERE name like "王_"
结果中 _ 只匹配一个字符。
SELECT * from score WHERE name like "_玉_"
-- 既不姓王 也不姓李的学员信息 select * from score where not name like "王%" AND not name like "李%"
统计相关
执行数学运算
在学生成绩表中,通过运算可以得到每位学员的总成绩。
操作符 |
说明 |
+ |
加法 |
- |
减法 |
* |
乘法 |
/ |
除法 |
-- 将两个值进行相加运算 SELECT name, python_score,sql_score, python_score+sql_score as 总成绩 FROM score
-- 总成绩大于250 的前3名 SELECT name,python_score,sql_score,test_score, python_score+sql_score+test_score as 总成绩 from score -- 总成绩大于等于250 WHERE python_score+sql_score+test_score >= 250 -- 进行排序 ORDER BY 总成绩 DESC -- 取前3个值 LIMIT 3
聚集函数
函数 |
说明 |
avg() |
返回某列的平均值 |
count() |
返回某列的行数 |
max() |
返回某列的最大值 |
min() |
返回某列的最小值 |
sum() |
返回某列之和 |
-- python成绩最高是多少 SELECT max(python_score) FROM score;
SELECT sum(python_score) as Python总成绩, MAX(python_score) as Python最高得分, MIN(python_score) as Python最低得分, AVG(python_score) as Python平均分, COUNT(python_score) as 参加Python考试的总人数 FROM score;
数据分组
使用上述聚集函数可以对数据进行进一步的操作。
-- python 成绩及格的同学总数 SELECT COUNT(name) FROM score WHERE python_score >= 60
-- python成绩及格或者sql成绩及格的学员总数 SELECT COUNT(name) FROM score WHERE python_score >= 60 OR sql_score >= 60
数据表的创建
在对应数据库上【新建表】
根据表结构信息创建数据表
使用sql语句创建
CREATE TABLE zhaozengyang_score -- 表名 ( -- 添加列名 id int not null auto_increment, name VARCHAR(255) not null, sql_score int(3) null, python_score int(3) null, test_score int(3) null, -- 声明id为主键 PRIMARY KEY(id) );
类型 |
说明 |
int |
整数数据 |
varchar |
可变长度的字符串 |
datatime |
日期 |
导入数据
可以从附件中下载对应的数据
选择要导入的文件格式
选择文件路径和编码
选择要导入的目标表
点击【开始】,导入成功之后有对应的提示。
练习
1.查询score表中Python成绩,SQL成绩,test成绩 中三个成绩中同时都大于60分的学员
SELECT name, python_score,sql_score,test_score from score WHERE python_score > 60 AND sql_score > 60 AND test_score > 60
2.查询score表中三科成绩都在80分到100分之间的学员信息
SELECT name, python_score,sql_score,test_score from score WHERE python_score BETWEEN 80 AND 100 AND sql_score BETWEEN 80 AND 100 AND test_score BETWEEN 80 AND 100;
3. 统计Python成绩在60分以上同学的Python总分数,以及这些成绩的平均分。
SELECT SUM(python_score), AVG(python_score) from score WHERE python_score >= 60