MySQL(4):CRUD语句(2)——基本查询
2016-12-26 16:59
567 查看
“增删改查”的查询语句。
create table student(
id int,
name varchar(20),
chinese float,
english float,
math float
);
insert into student(id,name,chinese,english,math) values(1,'张小明',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'李进',67,98,56);
insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'李一',88,98,90);
insert into student(id,name,chinese,english,math) values(5,'李来财',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'张进宝',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'黄蓉',75,65,30);
查询语句,一定是重点!
基本select语句
1. 基本语法
SELECT [DISTINCT] *|{column1, column2. column3..} FROM tableName;
select 指定查询哪些列的数据。
column指定列名。
*号代表查询所有列。
From指定查询哪张表。
DISTINCT可选,指显示结果时,是否剔除重复数据
select english from student;
select distinct english from student; -- 自动过滤重复的记录
指定列名
select name, english from student;
2. 在select语句中可使用表达式对查询的列进行运算
select name, english + chinese + english from student; -- 有表达式
select name, english + chinese + english + 10 from student;
sum怎么办…
sum 好像是把所有行加起来,不是把列加起来……
3. 在select语句中可使用as语句
select name as 姓名, english as 英语, math as 数学 from student;
(甚至可以不加 as)
4. where语句
select * from student where name = '张小明';
select * from student where english > 90;
select * from student where chinese+math+english > 200;
5. where中常用运算法
比较
<> 不等于号
!= 等号
between ... and ... -- 闭区间!开区间呢?
in (范围中) select * from student where math in (90,77);
like 模糊查询
like语句中,% 代表零个或多个任意字符,_ 代表一个字符,例first_name like ‘_a%’;
is null 查询为空 is not null 查询非空 【注意】不能写 = null
逻辑
select * from student where math <= 80;
select * from student where not (math > 90); -- 非
查询英语分数在80-90之间的同学。
select * from student where (english >= 80) and (english <=90);
select * from student where english between 80 and 90;
查询数学分数为89,90,91的同学。
select * from student where math in (89,90,91);
查询所有姓李的学生成绩。
select * from student where name like '李%';
select * from student where name like '李_'; -- (姓李的,且两个字的名字)
查询数学分>80,语文分>80的同学。
select * from student where (math > 80) and (chinese > 80);
6. order by
SELECT column1, column2. column3.. FROM table order by column asc|desc
Order by 指定排序的列,排序的列即可是表中的列名,也可以是select 语句后指定的列名。
asc 升序、desc 降序
ORDER BY 子句应位于SELECT语句的结尾。
对数学成绩排序后输出。
select * from student order by math desc;
select * from student order by math asc;
对总分排序后输出,然后再按从高到低的顺序输出
select name 姓名, (chinese + math + english) 总分 from student order by 总分 desc;
对姓李的学生成绩排序输出
select name 姓名, (chinese + math + english) 总分 from student where name like '李%' order by 总分 asc;
聚合函数
1. count函数
count(列名)返回某一列,行的总数【注意】 返回的是一个数!
统计一个班级共有多少学生?
select count(*) from student;
统计数学成绩大于90的学生有多少个?
select count(*) from student where math >= 90;
统计总分大于250的人数有多少?
select count(*) 总分大于250的人数 from student where (chinese + math + english) > 250;
2. sum函数
sum函数返回满足where条件的行的和【注意】 是从上往下合计! 不是横着加! 对行统计!
select sum(列名){,sum(列名)…} from tablename [WHERE where_definition] -- 列名的数据类型是数值型
统计一个班级数学总成绩?
select sum(name) 数学总成绩 from student;
统计一个班级语文、英语、数学各科的总成绩
select sum(chinese) 语文总成绩, sum(english) 英语总成绩, sum(math) 数学总成绩 from student;
统计一个班级语文、英语、数学的成绩总和
select sum(chinese + english + math) from student; -- 这样写!
select sum(chinese) + sum(english) + sum(math) from student; -- 这样也可以!
统计一个班级语文成绩平均分
select sum(chinese)/count(*) from student;
注意:对多列求和,“,”号不能少。
3. avg函数
AVG函数返回满足where条件的一列的平均值
求一个班级数学平均分
select avg(math) 数学平均分 from student; -- 注意,如果有一个空值null,会跳过这条记录。
求一个班级总分平均分
select avg(math + chinese + english) from student;
sum函数、avg函数都会忽略掉null?
4. max/min函数
max/min函数返回满足where条件的一列的最大/最小值
求班级最高分和最低分(数值统计)
select max(english+math+chinese) from student;
distinct 剔除重复结果
group by 分类
准备数据。
create table orders(
id int,
product varchar(20),
price float
);
insert into orders(id,product,price) values(1,'电视',900);
insert into orders(id,product,price) values(2,'洗衣机',100);
insert into orders(id,product,price) values(3,'洗衣粉',90);
insert into orders(id,product,price) values(4,'桔子',9);
insert into orders(id,product,price) values(5,'洗衣粉',90);
-- group by 的用法
-- 对订单表中商品归类后,显示每一类商品的总价
SELECT product, SUM(price) FROM orders GROUP BY product;
-- 注意,是对产品分类后的,进行sum。sum参与的是分组后的sum
having 聚合后筛选
-- having 过滤
-- 查询购买了几类商品,并且每类总价大于100的商品
SELECT product, SUM(price) FROM orders GROUP BY product HAVING (SUM(price)>100);
-- having 和 where均可实现过滤,但在having可以使用聚合函数,having通常跟在group by后,它作用于组。
-- 顺序:group by … having … order by …
-- 一共买了哪些类型的产品
SELECT product FROM orders GROUP BY product;
create table student(
id int,
name varchar(20),
chinese float,
english float,
math float
);
insert into student(id,name,chinese,english,math) values(1,'张小明',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'李进',67,98,56);
insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'李一',88,98,90);
insert into student(id,name,chinese,english,math) values(5,'李来财',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'张进宝',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'黄蓉',75,65,30);
查询语句,一定是重点!
基本select语句
1. 基本语法
SELECT [DISTINCT] *|{column1, column2. column3..} FROM tableName;
select 指定查询哪些列的数据。
column指定列名。
*号代表查询所有列。
From指定查询哪张表。
DISTINCT可选,指显示结果时,是否剔除重复数据
select english from student;
select distinct english from student; -- 自动过滤重复的记录
指定列名
select name, english from student;
2. 在select语句中可使用表达式对查询的列进行运算
select name, english + chinese + english from student; -- 有表达式
select name, english + chinese + english + 10 from student;
sum怎么办…
sum 好像是把所有行加起来,不是把列加起来……
3. 在select语句中可使用as语句
select name as 姓名, english as 英语, math as 数学 from student;
(甚至可以不加 as)
4. where语句
select * from student where name = '张小明';
select * from student where english > 90;
select * from student where chinese+math+english > 200;
5. where中常用运算法
比较
<> 不等于号
!= 等号
between ... and ... -- 闭区间!开区间呢?
in (范围中) select * from student where math in (90,77);
like 模糊查询
like语句中,% 代表零个或多个任意字符,_ 代表一个字符,例first_name like ‘_a%’;
is null 查询为空 is not null 查询非空 【注意】不能写 = null
逻辑
select * from student where math <= 80;
select * from student where not (math > 90); -- 非
查询英语分数在80-90之间的同学。
select * from student where (english >= 80) and (english <=90);
select * from student where english between 80 and 90;
查询数学分数为89,90,91的同学。
select * from student where math in (89,90,91);
查询所有姓李的学生成绩。
select * from student where name like '李%';
select * from student where name like '李_'; -- (姓李的,且两个字的名字)
查询数学分>80,语文分>80的同学。
select * from student where (math > 80) and (chinese > 80);
6. order by
SELECT column1, column2. column3.. FROM table order by column asc|desc
Order by 指定排序的列,排序的列即可是表中的列名,也可以是select 语句后指定的列名。
asc 升序、desc 降序
ORDER BY 子句应位于SELECT语句的结尾。
对数学成绩排序后输出。
select * from student order by math desc;
select * from student order by math asc;
对总分排序后输出,然后再按从高到低的顺序输出
select name 姓名, (chinese + math + english) 总分 from student order by 总分 desc;
对姓李的学生成绩排序输出
select name 姓名, (chinese + math + english) 总分 from student where name like '李%' order by 总分 asc;
聚合函数
1. count函数
count(列名)返回某一列,行的总数【注意】 返回的是一个数!
统计一个班级共有多少学生?
select count(*) from student;
统计数学成绩大于90的学生有多少个?
select count(*) from student where math >= 90;
统计总分大于250的人数有多少?
select count(*) 总分大于250的人数 from student where (chinese + math + english) > 250;
2. sum函数
sum函数返回满足where条件的行的和【注意】 是从上往下合计! 不是横着加! 对行统计!
select sum(列名){,sum(列名)…} from tablename [WHERE where_definition] -- 列名的数据类型是数值型
统计一个班级数学总成绩?
select sum(name) 数学总成绩 from student;
统计一个班级语文、英语、数学各科的总成绩
select sum(chinese) 语文总成绩, sum(english) 英语总成绩, sum(math) 数学总成绩 from student;
统计一个班级语文、英语、数学的成绩总和
select sum(chinese + english + math) from student; -- 这样写!
select sum(chinese) + sum(english) + sum(math) from student; -- 这样也可以!
统计一个班级语文成绩平均分
select sum(chinese)/count(*) from student;
注意:对多列求和,“,”号不能少。
3. avg函数
AVG函数返回满足where条件的一列的平均值
求一个班级数学平均分
select avg(math) 数学平均分 from student; -- 注意,如果有一个空值null,会跳过这条记录。
求一个班级总分平均分
select avg(math + chinese + english) from student;
sum函数、avg函数都会忽略掉null?
4. max/min函数
max/min函数返回满足where条件的一列的最大/最小值
求班级最高分和最低分(数值统计)
select max(english+math+chinese) from student;
distinct 剔除重复结果
group by 分类
准备数据。
create table orders(
id int,
product varchar(20),
price float
);
insert into orders(id,product,price) values(1,'电视',900);
insert into orders(id,product,price) values(2,'洗衣机',100);
insert into orders(id,product,price) values(3,'洗衣粉',90);
insert into orders(id,product,price) values(4,'桔子',9);
insert into orders(id,product,price) values(5,'洗衣粉',90);
-- group by 的用法
-- 对订单表中商品归类后,显示每一类商品的总价
SELECT product, SUM(price) FROM orders GROUP BY product;
-- 注意,是对产品分类后的,进行sum。sum参与的是分组后的sum
having 聚合后筛选
-- having 过滤
-- 查询购买了几类商品,并且每类总价大于100的商品
SELECT product, SUM(price) FROM orders GROUP BY product HAVING (SUM(price)>100);
-- having 和 where均可实现过滤,但在having可以使用聚合函数,having通常跟在group by后,它作用于组。
-- 顺序:group by … having … order by …
-- 一共买了哪些类型的产品
SELECT product FROM orders GROUP BY product;
相关文章推荐
- MYSQL基本查询语句练习2--GROUP BY HAVING COUNT 查找分数表中一个学生同一课程有两条数据
- MySQL基本SQL语句之单表查询、多表查询和子查询
- MySQL(7):CRUD语句(3)——子查询和多表查询、蠕虫复制
- MySQL表格查询基本语句2
- <转>MySQL查询语句练习题,测试基本够用了
- MySQL基本查询语句练习
- MySQL基本查询语句
- mysql基本查询语句review
- 跟王老师学MySQL: 基本查询语句
- MySQL查询表格内容基本语句1
- MySQL基本SQL语句之单表查询、多表查询和子查询
- MySQL中的基本查询语句学习笔记
- mysql去除重复查询的SQL语句基本思路
- mysql基本查询语句review-2
- Mysql多表查询 --一个例子基本搞通mysql语句
- mysql的基本查询语句
- MySQL中的基本查询语句学习笔记
- Mysql 增减查询基本语句
- MYSQL基本查询语句DQL