数据库02

数据操作-查询

创建数据表

drop table if exists students;
create table students ( 
studentNo varchar(10) primary key,  
name varchar(10),  
sex varchar(1),  
hometown varchar(20),  
age tinyint(4),  
class varchar(10),  
card varchar(20) 
)

准备数据

insert into students values (
'001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'), 
('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'), 
('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'), 
('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'), 
('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'), 
('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
('008', '小乔', '女', '河南', '15', '3班', null), 
('009', '百里守约', '男', '湖南', '21', '1班', ''), 
('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')
  • 查询所有字段
    select * from 表名
    例: select * from students

  • 查询指定字段
    在select后面的列名部分,可以使用as为列起别名,这个别名出现在结果集中

select 列1,列2,... from 表名
-- 表名.字段名 select students.name,students.age from students
-- 可以通过 as 给表起别名 select s.name,s.age from students as s
-- 如果是单表查询 可以省略表明 select name,age from students
- 使用as给字段起别名 select studentNo as 学号,name as 名字,sex as 性别 from students

消除重复行

在select后面列前使用distinct可以消除重复的行
select distinct 列1,... from 表名;
例: select distinct sex from students;

条件

使用where子句对表中的数据筛选,符号条件的数据会出现在结果集中 语法如下:
select 字段1,字段2... from 表名 where 条件;
例: select * from students where id=1;

  • where后面支持多种运算符,进行条件的处理
    • 比较运算
    • 逻辑运算
    • 模糊查询
    • 范围查询
    • 空判断

比较运算符

  • 等于: =
  • 大于: >
  • 大于等于: >=
  • 小于: <
  • 小于等于: <=
  • 不等于: != 或 <>

例1:查询小乔的年龄
select age from students where name='小乔'
例2:查询20岁以下的学生
select * from students where age<20
例3:查询家乡不在北京的学生
select * from students where hometown!='北京'

逻辑运算符

  • and or not

例1:查询年龄小于20的女同学
select * from students where age<20 and sex='女'
例2:查询女学生或'1班'的学生
select * from students where sex='女' or class='1班'
例3:查询非天津的学生
select * from students where not hometown='天津'

模糊查询

  • like %表示任意多个任意字符 _表示一个任意字符

例1:查询姓孙的学生
select * from students where name like '孙%'
例2:查询姓孙且名字是一个字的学生
select * from students where name like '孙_'
例3:查询叫乔的学生
select * from students where name like '%乔'
例4:查询姓名含白的学生
select * from students where name like '%白%'

范围查询

  • in表示在一个非连续的范围内

例1:查询家乡是北京或上海或广东的学生
select * from students where hometown in('北京','上海','广东')

  • between ... and ...表示在一个连续的范围内

例2:查询年龄为18至20的学生
select * from students where age between 18 and 20

空判断

  • 注意:null与''是不同的
  • 判空is null

例1:查询没有填写身份证的学生
select * from students where card is null
判非空is not null

例2:查询填写了身份证的学生
select * from students where card is not null

排序

  • 为了方便查看数据,可以对数据进行排序
    语法:
    select * from 表名 order by 列1 asc|desc,列2 asc|desc,...
    将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推 默认按照列值从小到大排列 asc从小到大排列,即升序 desc从大到小排序,即降序

例1:查询所有学生信息,按年龄从小到大排序
select * from students order by age

例2:查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序
select * from students order by age desc,studentN

例2:查询所有学生信息,按姓名(中文)从小到大排序
select * from students order by convert(name using gbk)

聚合函数

  • 为了快速得到统计数据,经常会用到如下5个聚合函数
  • count(*)表示计算总行数,括号中写星与列名,结果是相同的
  • 聚合函数不能在 where 中使用

例1:查询学生总数
select count(*) from students;

  • max(列)表示求此列的最大值
    例2:查询女生的最大年龄
    select max(age) from students where sex='女';

  • min(列)表示求此列的最小值
    例3:查询1班的最小年龄
    select min(age) from students;

  • sum(列)表示求此列的和
    例4:查询北京学生的年龄总和
    select sum(age) from students where hometown='北京';

  • avg(列)表示求此列的平均值
    例5:查询女生的平均年龄
    select avg(age) from students where sex='女'

分组

  • 按照字段分组,表示此字段相同的数据会被放到一个组中

  • 分组后,分组的依据列会显示在结果集中,其他列不会显示在结果集中

  • 可以对分组后的数据进行统计,做聚合运算

  • 语法:
    select 列1,列2,聚合... from 表名 group by 列1,列2...

例1:查询各种性别的人数
select sex,count(*) from students group by sex

例2:查询各种年龄的人数
select age,count(*) from students group by age

分组后的数据筛选

  • 语法:
    select 列1,列2,聚合... from 表名 group by 列1,列2,列3... having 列1,...聚合...
    having后面的条件运算符与where的相同

例:查询男生总人数
方案一 select count(*) from students where sex='男'
方案二: select sex,count(*) from students group by sex having sex='男'

  • 对比where与having
    where是对from后面指定的表进行数据筛选,属于对原始数据的筛选 having是对group by的结果进行筛选

获取部分行

当数据量过大时,在一页中查看数据是一件非常麻烦的事情
语法
select * from 表名 limit start,count
从start开始,获取count条数据 start索引从0开始

例:查询前3行学生信息
select * from students limit 0,3

分页

已知:每页显示m条数据,求:显示第n页的数据
select * from students limit (n-1)*m,m

  • 求总页数
    • 查询总条数p1
    • 使用p1除以m得到p2
    • 如果整除则p2为总数页
    • 如果不整除则p2+1为总页数

连接查询

准备数据

drop table if exists courses; 
create table courses (  
courseNo int(10) unsigned primary key auto_increment,  
name varchar(10) 
);

insert into courses values 
('1', '数据库'), 
('2', 'qtp'), 
('3', 'linux'), 
('4', '系统测试'), 
('5', '单元测试'), 
('6', '测试过程');

drop table if exists scores; 
create table scores (  
id int(10) unsigned primary key auto_increment,  
courseNo int(10),  
studentno varchar(10),  
score tinyint(4) 
);

insert into scores values 
('1', '1', '001', '90'), 
('2', '1', '002', '75'), 
('3', '2', '002', '98'), 
('4', '3', '001', '86'), 
('5', '3', '003', '80'), 
('6', '4', '004', '79'), 
('7', '5', '005', '96'), 
('8', '6', '006', '80');

等值连接

方式一
select * from 表1,表2 where 表1.列=表2.列
此方式会产生笛卡尔积, 生产记录总数=表1总数*表2总数
会产生临时表

方式二(又称内连接)
select * from 表1 inner join 表2 on 表1.列=表2.列
不会产生笛卡尔积, 不会产生临时表, 性能高

  • 例1:查询学生信息及学生的成绩
select  
    * 
from    
    students stu,    
    scores sc 
where    
    stu.studentNo = sc.studentNo


select    
    * 
from    
    students stu inner join scores sc on stu.studentNo = sc.studentNo
  • 例2:查询课程信息及课程的成绩
select    
    * 
from    
    courses cs,    
    scores sc 
where    
    cs.courseNo = sc.courseNo

select    
    * 
from    
    courses cs inner join scores sc on cs.courseNo = sc.courseNo
  • 例3:查询学生信息及学生的课程对应的成绩
select    
    * 
from    
    students stu,    
    courses cs,    
    scores sc 
where    
    stu.studentNo = sc.studentno    
    and cs.courseNo = sc.courseNo

select    
* 
from    
students stu 
inner join scores sc on stu.studentNo = sc.studentNo 
inner join courses cs on cs.courseNo = sc.courseNo
  • 例4:查询王昭君的成绩,要求显示姓名、课程号、成绩
select    
    stu.name,    
    sc.courseNo,    
    sc.score
from    
    students stu,    
    scores sc 
where    
    stu.studentNo = sc.studentNo    
    and stu.name = '王昭君'

select    
    stu.name,    
    sc.courseNo,    
    sc.score 
from    
    students stu 
inner join scores sc on stu.studentNo = sc.studentNo 
where    
    stu.name = '王昭君'
  • 例5:查询王昭君的数据库成绩,要求显示姓名、课程名、成绩
select    
    stu.name,    
    cs.name,    
    sc.score 
from    
    students stu,    
    scores sc,    
    courses cs 
where    
    stu.studentNo = sc.studentNo    
    and sc.courseNo = cs.courseNo    
    and stu.name = '王昭君'    
    and cs.name = '数据库'

select    
stu.name,    
cs.name,    
sc.score 
from    
students stu 
inner join scores sc on stu.studentNo = sc.studentNo 
inner join courses cs on sc.courseNo = cs.courseNo 
where    
    stu.name = '王昭君' and cs.name = '数据库'
  • 例6:查询所有学生的数据库成绩,要求显示姓名、课程名、成绩
select    
    stu.name,   
    cs.name,    
    sc.score    
from    
    students stu,    
    scores sc,    
    courses cs 
where    
    stu.studentNo = sc.studentNo    
    and sc.courseNo = cs.courseNo    
    and cs.name = '数据库'

select    
    stu.name,    
    cs.name,    
    sc.score 
from    
    students stu 
inner join scores sc on stu.studentNo = sc.studentNo 
inner join courses cs on sc.courseNo = cs.courseNo 
where   
    cs.name = '数据库'
  • 例7:查询男生中最高成绩,要求显示姓名、课程名、成绩
select    
    stu.name,   
    cs.name,    
    sc.score 
from    
    students stu,    
    scores sc,    
    courses cs 
where    
    stu.studentNo = sc.studentNo    
    and sc.courseNo = cs.courseNo    
    and stu.sex = '男' 
order by    
    sc.score desc 
limit 1

select    
    stu.name,    
    cs.name,    
    sc.score 
from    
    students stu 
    inner join scores sc on stu.studentNo = sc.studentNo 
    inner join courses cs on sc.courseNo = cs.courseNo 
where    
    stu.sex = '男' 
order by    
    sc.score desc 
limit 1
posted @ 2020-02-23 15:16  20145232  阅读(114)  评论(0编辑  收藏  举报