
MySQL的select详细介绍
发布日期:2021-05-07 16:40:08
浏览次数:24
分类:技术文章
本文共 3297 字,大约阅读时间需要 10 分钟。
MySQL 查询数据
MySQL 数据库使用SQL SELECT语句来查询数据。
你可以通过 mysql> 命令提示窗口中在数据库中查询数据
语法
以下为在MySQL数据库中查询数据通用的 SELECT 语法:
SELECT column_name,column_nameFROM table_name[WHERE Clause][LIMIT N][ OFFSET M]
- 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
- SELECT 命令可以读取一条或者多条记录。
- 你可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
- 你可以使用 WHERE 语句来包含任何条件。
- 你可以使用 LIMIT 属性来设定返回的记录数。
- 你可以通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。
建一张表用于我们测试:
create table student( ids int auto_increment primary key, name varchar(20), chinese float, english float, math float );插入如下数据:
insert into student values(1,'李明',89,78,90); insert into student values(2,'乘风',67,89,56); insert into student values(3,'南宫流云',87,78,77); insert into student values(4,'南宫皓月',88,98,90); insert into student values(5,'南宫紫月',82,84,67); insert into student values(6,'萧炎',55,85,45); insert into student values(7,'林动',75,65,30);
1、指定查询列
mysql> select id,name,chinese from student;
2、去重查询
用distinct关键字, 如果结果中有完全相同的行,就去除重复行
mysql> select distinct math from student;
3、select语句中进行运算
查询学生总成绩
mysql> select id,name,(chinese+math+english) as 总成绩 from student;
查询所有姓南宫人的总成绩。
mysql> select id,name,(chinese+math+english) as 总成绩 from student -> where name like '南宫%';
4、where查询过滤
在where子句中有很多经常使用的运算符,如下:
(1)查询所有英语成绩大于90的同学成绩:
mysql> select id,name,english as 英语 from student -> where english > 90;
(2)查询所有总分大于200分的同学
注意:where子句后不能用别名 因为数据库中先执行where子句,再执行select子句。
mysql> select id,name,(chinese+math+english) as 总成绩 from student -> where (chinese+math+english) > 200;
(3)查询姓林并且id大于6的学生信息
mysql> select id,name from student -> where name like '林%' and id >6;
(4)查询英语成绩大于语文成绩的同学
mysql> select id,name from student -> where english > chinese;
(5)查询所有总分大于200并且数学成绩小于语文成绩的学生信息
mysql> select id,name from student -> where (chinese+math+english) >200 and math < chinese;
(6)查询所有英语成绩在80到90分的同学
方法一:mysql> select id,name,english from student -> where english >=80 and english <= 90;
方法二:
注意:between是闭区间
mysql> select id,name,english from student -> where english between 80 and 90;
(7)查询数学成绩为89,90,91的同学信息
or:mysql> select id,name,math from student -> where math=89 or math=90 or math=91;
in:
mysql> select id,name,math from student -> where math in(89,90,91);
5、order by排序语句
asc升序(默认),desc降序
order by 子句应该位于select语句的结尾 eg:对数学成绩进行排序 默认升序:mysql> select id,name,math from student -> order by math;
降序:
mysql> select id,name,math from student -> order by math desc;
对总分进行从高到低输出
mysql> select (chinese+math+english) as 总成绩 from student -> order by 总成绩 desc;
6、常用函数
(1)count()
count(*)统计null值 count(列名)排除null值 eg :统计当前student表中一共有多少学生mysql> select count(*) as 人数 from student;
(2)sum()
eg:统计一个班数学总成绩mysql> select sum(math) as 数学总成绩 from student;
(3)平均值:avg()
求数学的平均值
mysql> select sum(math)/count(*) as 数学平均值 from student;
mysql> select avg(math) as 数学平均值 from student;
7、group by 子句的使用
假设有一个职工信息表, EMP:表名 ;部门:depton;sal:工资;job:工作 我们设想: (1)显示每个部门的平均工资和最高工资 select deptno,avg(sal),max(sal) from EMP group by deptno; (2)显示每个部门的每种岗位的平均工资和最低工资 select avg(sal),min(sal),job, deptno from EMP group by deptno, job; 补充:首先按照deptno分组,然后各组再按照job进行分组。 (3)显示平均工资低于2000的部门和它的平均工资 解题思路: 1. 统计各个部门的平均工资 select avg(sal) from EMP group by deptno 2. having往往和group by配合使用,对group by结果进行过滤 select avg(sal) as myavg from EMP group by deptno having myavg<2000;
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年03月22日 11时01分07秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
一篇文章带你搞定官方推荐 Stack 的替代品 双端队列 Deque
2019-03-04
(LeetCode)Java 求解搜索旋转排序数组
2019-03-04
(模拟数组)Java 求解螺旋矩阵 II
2019-03-04
Burpsuite-02-设置JVM内存大小与解决页面显示文字乱码错误
2019-03-04
Python学习:字符串
2019-03-04
计算几何(旁切圆) - Ex-circles - UVA 11731
2019-03-04
DP - Tickets - HDU - 1260
2019-03-04
phpStudy for Linux (lnmp+lamp一键安装包)
2019-03-04
【安卓学习笔记】JAVA基础Lesson9-对象的转型
2019-03-04
本校暑假训练营11_Python数据分析入门7-网络1
2019-03-04
本校暑假训练营12_Python数据分析入门7-网络2
2019-03-04
数据库SQL实战3_获取所有非manager的员工emp_no
2019-03-04
LeetCode7_数组双指针_有序数组元素去重、数组移除指定元素
2019-03-04
JVM篇-结合源码分析垃圾收集器的类型
2019-03-04
RT -Thread Studio开发环境下使用W5500做TCP客户端
2019-03-04
Warning: The core is locked up的解决办法
2019-03-04
奔涌吧 后浪!!! 哔哩哔哩 何冰
2019-03-04