赞
踩
基本查询
语法:select [distinct] *|列名 from 表 [条件];
环境的准备:
create table exam(
id int primary key auto_increment,
name varchar(20),
english int,
chinese int,
math int
);
insert into exam values (null,'张三',85,74,91);
insert into exam values (null,'李四',95,90,83);
insert into exam values (null,'王五',85,84,59);
insert into exam values (null,'赵六',75,79,76);
insert into exam values (null,'田七',69,63,98);
insert into exam values (null,'李老八',89,90,83);
查询所有学生考试成绩信息
select * from exam;
查询所有学生的姓名和英语成绩
select name,english from exam;
查询英语成绩信息(不显示重复的值)
select distinct english from exam;
查看学生姓名和学生的总成绩
select name,english+chinese+math from exam;
别名查询
select name,english+chinese+math as sum from exam;
条件查询
使用where子句
> , < , >= , <= , <> ,=
like:模糊查询
in:范围查询
条件关联:and , or ,not
查询李四学生的成绩:
select * from exam where name = '李四';
查询名称叫李四学生并且英文大于90分
select * from exam where name = '李四' and english > 90;
查询姓李的学生的信息
like可以进行模糊查询,在like子句中可以使用_或者%作为占位符。只能代表一个字符,而%可以代表任意个字符。
* like ‘李’ :名字中必须是两个字,而且是姓李的。
* like ‘李%’ :名字中姓李的学生,李子后可以是1个或任意个字符。
* like ‘%四’ :名字中以四结尾的。
* like ‘%王%’ :只要名称中包含这个字就可以。
select * from exam where name like '李%';
查询英语成绩是69,75,89学生的信息
select * from exam where english in (69,75,89);
排序查询
使用order by 字段名称 asc/desc;
查询学生信息,并且按照语文成绩进行排序:
select * from exam order by chinese;
查询学生信息,并且按照语文成绩倒序排序:
select * from exam order by chinese desc;
查询学生信息,先按照语文成绩进行倒序排序,如果成绩相同再按照英语成绩升序排序
select * from exam order by chinese desc,english asc;
查询姓李的学生的信息,按照英语成绩降序排序
select * from exam where name like '李%' order by english desc;
分组统计查询
聚合函数使用
sum();
获取所有学生的英语成绩的总和:
select sum(english) from exam;
获取所有学生的英语成绩和数学成绩总和:
select sum(english),sum(math) from exam;
查询姓李的学生的英语成绩的总和
select sum(english) from exam where name like '李%';
查询所有学生各科的总成绩:
select sum(english)+sum(chinese)+sum(math) from exam;
select sum(english+chinese+math) from exam;
与上面的语句有什么不同?
select count(*) from exam;
获得姓李的学生的个数
select count(*) from exam where name like '李%';
max();
获得数学成绩的最高分:
select max(math) from exam;
min();
获得语文成绩的最小值
select min(chinese) from exam;
avg();
获取语文成绩的平均值
select avg(chinese) from exam;
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。