MySQL学习之查询(一)

语法

1.查询语法
(1)select * from 表; —默认查询所有字段
(2)select 字段1,字段2 … from 表; —查询指定的字段
(3)distinct —去除重复
(4)基本的运算
(5)函数
(6)起别名
(7)查询中可以给指定的表或者字段起别名,使用as关键字,来简化写法
select math as m,english as e ,chinese as c from student as stu;
也可以不写as
select math m,english e ,chinese c from student stu;

(8)where 条件限定,走一些过滤
select * from student where math = 90; —数学考了90分的所有人

2.练习
create table stu(
id int auto_increment primary key,
name varchar(10) not null,
math int,
chinese int,
english int
);

insert into student(name,math,chinese,english) values (‘小明’,90,98,95);
insert into student(name,math,chinese,english) values (‘小红’,93,90,97);
insert into student(name,math,chinese,english) values (‘张三’,70,98,85);
insert into student(name,math,chinese,english) values (‘张四’,60,78,87);
insert into student(name,math,chinese,english) values (‘王小二’,92,77,82);

(1) 查询所有学生信息;
select * from student;

(2)查询所有学生的姓名,数学成绩信息;
select name, math from student;

(3)查询所有学生的姓名,数学、英语成绩信息;
select name, math,english from student;

(4)过滤表中数学重复数据
select distinct math from student;

(5)查询所有学生的各科成绩,并且都加2分
select name,math+2,english+2,chinese+2 from student;

(6)查询所有学生的总成绩
select name,math+english+chinese from student;

(7)使用别名进行查询学生的成绩
select name,math as 数学成绩 ,english as 英语成绩 from student;

(8)查询张三的数学成绩
select name,math from student where name=‘张三’;

(9)查询数学成绩大于90分的同学
select * from student where math>90;

(10)查询总分大于270的学生
select * from student where math+english+chinese> 270;

select name,( math+english+chinese) as total from student where total> 270; //错误的,别名发生在where之后,先运行where语句的时候还没有total

(11)查询所有学生的所有科目中最高的分数

where 后常用的符号

  1. (大于) <(小于) >=(大于等于) <=(小于等于) <>(不等于)

  2. in —(表示从一个范围内选择)
    select * from student where math in (90,78,70,92);//查询数学成绩有么有等于这些值得同学的信息

  3. like 模糊匹配
    like ‘小_’ —代表单个占位符
    like ‘小%’ —代表多个占位符
    like ‘%小’
    like ‘%小%’

  4. and

  5. or

  6. not

  7. between … and … —取值范围都是闭区间,[]

  8. 练习
    (1)查询数学成绩大于80分的学生信息
    select * from student where math > 80;

(2)查询数学成绩有么有90分,92分的学生信息。
select * from student where math in(90,92);

(3)查询姓张的同学信息
select * from student where name like’张%’;
select * from student where name like’张_’; —这个会漏数据,只会查询出来2个长度的姓张的学生信息

(4)查询英语成绩在80分以上并且姓张的学生信息
select * from student where english > 80 and name like ‘张%’;

(5)查询数学大于80分并且语文大于80分的学生信息
select * from student where math >80 and chinese > 80;

(6)查询总分大于270的学生信息
select * from student where (math+english+chinese) > 270;

(7)查询数学成绩在80-95分之间的学生信息
select * from student where math between 80 and 95;
select * from student where math>=80 and math<=95;

order by 对结果进行排序

  1. desc | asc ,降序 | 升序,不写的话是默认升序

  2. order by 放在select 语句的末尾

  3. 练习
    (1)对英语成绩进行排序输出
    select * from student order by english; —默认升序

(2)对语文成绩降序后输出
select * from student order by chinese desc; —降序

(3)按照总分进行降序
select *, (math+english+chinese) as 总分 from student order by (math+english+chinese) desc;

(4)按照数学成绩排序降序,如数学成绩相同,按照语文成绩降序
select name,math,chinese from student order by math desc,chinese desc;

聚合函数

操作的都是一行或者一列,聚合函数操作的字段也是可以做简单±*/运算的

  1. count —统计个数

  2. sum —统计求和,求某一列数据的和,没有sum(*)这样的写法,对非数值类型也不起作用。

  3. avg —统计求平均

  4. max —统计求最大值

  5. min —统计求最小值

  6. 练习
    (1)统计一个班级有多少学生
    select count(*) from student;
    select count(name) from student;

(2)统计数学成绩大于90的学生有多少个
select count(*) from student where math > 90;

(3)统计总分大于270的有多少人
select count(*) from student where math+english+chinese>270;

(4)统计一个班的数学总成绩
select sum(math) from student;

(5)统计一个班的语文、数学、英语各科总成绩
select sum(chinese),sum(math),sum(english) from student;

(6)统计一个班的语文、数学、英语总成绩之和
select sum(math+chinese+english) from student;
select sum(math)+sum(chinese)+sum(english) from student;

(7)求一个班的英语平均分
select sum(english)/count(*) from student;
select avg(english) from student;

注意:
count 、sum、avg会忽略null值

(8)求一个班的英语平均分
select avg(english) from student;

(9)求一个班的总平均分
select avg(math+chinese+english) from student;

(10)求班级最高分
select max(math+english+chinese) from student;

(11)求班级最低分
select min(math+english+chinese) from student;

分组查询 group by ,聚合函数一般都是和分组一起用的

group by 字段
select * from student; —默认其实也是有分组的,分成1组
select * from group by sex; —按照性别分组

having是用于分组后的一些条件过滤的关键词,可以配合聚合函数一起使用

  1. 练习
    create table orders(
    id int auto_increment primary key,
    product varchar(10),
    price float
    );

insert into orders values(null,‘台灯’,99);
insert into orders values(null,‘台灯’,101);
insert into orders values(null,‘书桌’,190);
insert into orders values(null,‘书桌’,220);
insert into orders values(null,‘手机’,1990);
insert into orders values(null,‘手机’,1990);
insert into orders values(null,‘手机’,5880);
insert into orders values(null,‘电饭锅’,599);

(1)显示每一类商品的总价
select product, sum(price) from orders group by product;

(2)查询购买了几类商品,并且没类总价大于500的商品
select product,sum(price),count(product) from orders group by product having sum(price)>500;

(3)查询购买了几类商品,并且商品价格要大于150,每类总价大于500的商品
select product,sum(price),count(product) from orders where price>100 group by product having sum(price)>199;

总结:
where 关键字后不能使用聚合函数,而having可以
where的执行先于group by,先对where进行条件过滤后然后再分组

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值