创建student表
CREATE TABLE student(
id int not null auto_increment primary key,
code varchar(10) not null,
name varchar(20) not null,
sex varchar(2),
birth date,
school varchar(20) not null,
class varchar(10) not null,
location varchar(16) not null,
score float not null default 0.0,
unique index codeidex(code asc)
);
效果如下图:
23 08133192 李飞 男 1985-10-01 计算机学院 2013-1 山西太原 526 24 08133193 李大伟 男 1986-10-01 计算机学院 2013-2 山西交口 568 25 08133194 李霞 女 1986-09-01 计算机学院 2013-3 江苏徐州 525 26 08133195 王霞 女 1986-08-01 计算机学院 2013-4 江苏南京 528 27 08133196 王俊 男 1985-08-01 计算机学院 2013-5 北京 548 28 08133197 王飞 男 1985-11-01 计算机学院 2013-4 陕西西安 558 29 08133198 宋小倩 女 1985-12-12 计算机学院 2013-1 江苏杨州 598 30 08133199 宋小俊 男 1985-01-12 计算机学院 2013-2 江苏南通 578 31 081331910 王静 女 1985-08-12 计算机学院 2013-3 江苏南通 508
创建class_table表:
效果如下:
23 08133192 李飞 计算机学院 2013-1 数据结构 88 24 08133192 李飞 计算机学院 2013-1 数据库原理 98 25 08133192 李飞 计算机学院 2013-1 微机原理 98 26 081331910 王静 计算机学院 2013-3 微机原理 98 27 081331910 王静 计算机学院 2013-3 数据库原理 98 28 081331910 王静 计算机学院 2013-3 数据结构 78 29 08133193 李大伟 计算机学院 2013-2 数据结构 82 30 08133193 李大伟 计算机学院 2013-2 数据库原理 86 31 08133193 李大伟 计算机学院 2013-2 微机原理 80 32 08133198 宋小倩 计算机学院 2013-1 微机原理 81 33 08133198 宋小倩 计算机学院 2013-1 数据库原理 87 34 08133198 宋小倩 计算机学院 2013-1 数据结构 68 35 081331910 王静 计算机学院 2013-3 web设计 98 36 081331910 王静 计算机学院 2013-3 C#程序设计 98 37 081331910 王静 计算机学院 2013-3 ASP.net程序设计 98 38 081331910 王静 计算机学院 2013-3 马克思主义 98 39 081331910 王静 计算机学院 2013-3 英语1 98 40 081331910 王静 计算机学院 2013-3 高等数学 98 41 081331910 王静 计算机学院 2013-3 概率论 98 42 081331910 王静 计算机学院 2013-3 线性代数 98
1、普通查询
查询学生表中所有信息
select * from student;
2、查询特定行
查找姓名为王静的学生的情况
select * from student where name='王静';
3、查询特定列
查询学生姓名为王静的性别(查询结果的 同时显示姓名)
select name,sex from student where name='王静';
4、条件查询
- 查找性别为男并且分数大于528分的学生情况。
select * from student where sex='男' and score>528;
- 查找姓名不是王静的学生情况。
select * from student where name<>'王静';
- 查找分数大于等于545分的学生情况。
select * student where score>545;
- 查找姓名为王静或者姓名为李霞的学生情况。
select * from student where name='王静'
5、模糊查询
- 查询姓名中带有"李"字的学生情况。
select * from student where name like '%李%';
- 查询姓‘李’且姓名为两个字的学生情况。
select * from student where name like '李_';
6、查询排序
- 按分数升序排序
select * from student order by score asc;
- 按分数降序排序
select * from student order by score desc;
7、查询分组与统计
- 对学生表按班级分组。
select * from student group by class;
- 统计每班的学生姓名。
select class,GROUP_CONCAT(name) from student group by class;
- 统计每班的人数。
select class,count(*) from student group by class;
4. 统计每班的总分。select class,sum(score) from student group by class;
- 统计每班的最高分。
select class,max(score) from student group by class;
- 统计每班的总分,列出总分超过1100分的班级及其总分。
select class,sum(score) as sum from student group by class having sum(score)>1100;
7. 统计每班的总分,并汇总求和。select class,sum(score) as sum from student group by class with rollup;
8、多表查询
- 在student 表和class_table 表中查询姓名为王静的学生情况。
select student.*, class_table.* from student join class_table on student.name = class_table.name where student.name='王静';
- 在student 表和class_table 表中查询姓名相同的所有记录。
左连接
select student.*,class_table.classname,class_table.score from student left join class_table on student.name=class_table.name;
右连接
select class_table.*,student.name,student.sex from class_table right join student on student.name=class_table.name;