当前位置:网站首页>【sql语句基础】——查(select)(单表查询)
【sql语句基础】——查(select)(单表查询)
2022-06-12 18:16:00 【InfoQ】
查(select)
单表查询
基本语法
select [distinct] * | {column1,column2,column3...}
from table_name;
表


代码样例
-- 查询所有学生信息
SELECT * FROM student;
-- 查询所有学生姓名和英语成绩
SELECT `name`,`english` FROM student;
-- 统计总分别名score
SELECT `name` ,(chinese+english+math) AS score FROM student;
select注意事项
- select指定查询那些列的数据。
- column指定列名。
- *代表查询所有列。
- from指定查询哪些表。
- distinct可选,指显示结果时,是否去掉重复数据。
where子句
- 大于等于小于不等于
- between...and->显示在某一区间的值
- in(set)->显示在某一区间的值,例如in(100,200)
- 通配符:like `张%(0-多)``/not like ''【%代表多个字符,_代表单个字符】
- is null ->判断是否为空
- and->判断条件同时成立
- or—> 多个条件任一成立
- not—> 不成立,例如:where not(salary>100)
SELECT * FROM student
WHERE `name` = '赵云';
-- 查询总分大于200并且数学小于语文,姓韩的同学
SELECT * FROM student
WHERE (chinese+english+math) > 200 AND
math < chinese AND
`name` LIKE '赵%';
-- english 在80-90之间
SELECT * FROM student
WHERE english BETWEEN 80 AND 90;
排序order by子句
-- 数学成绩升序
SELECT * FROM student
ORDER BY math;
-- 总分降序
SELECT `name`,(chinese+math+english) AS score FROM student
ORDER BY score DESC;
-- 李姓成绩排序
SELECT * FROM student
WHERE `name` LIKE '张%'
ORDER BY math;
-- 先部门号排序,后工资降序
SELECT * FROM emp
ORDER BY deptno ,sal DESC;
合计/统计函数-count
-- 统计所以总分大于250
SELECT COUNT(*) AS s FROM student
WHERE (math+chinese+english)>250;
求和sum
-- 语文平均分
SELECT SUM(chinese)/COUNT(*) FROM student;
平均值avg
SELECT AVG(math+chinese+english) FROM student;
最大值最小值max和min
select max(math) from student;
分组group by
-- 每种岗位的雇员总数、平均工资
SELECT AVG(sal),COUNT(*),job
FROM emp
GROUP BY job;
-- 显示雇员总数以及获得补助的雇员总数
SELECT COUNT(*),COUNT(comm=300)
FROM emp;
-- 显示管理人数(去重)
SELECT COUNT(DISTINCT mgr)
FROM emp;
过滤having
-- GROUP by用于对查询的结果分组统计
-- 如何显示每个部门的平均工资和最高工资
SELECT AVG(sal), MAX(sal) , deptno
FROM emp GROUP BY deptno;
-- 使用数学方法,对小数点进行处理
SELECT FORMAT(AVG(sal),2), MAX(sal) , deptno
FROM emp GROUP BY deptno;
-- 显示每个部门的每种岗位的平均工资和最低工资
SELECT AVG(sal), MIN(sal) , deptno, job
FROM emp GROUP BY deptno, job;
-- ?显示平均工资低于2000的部门号和它的平均工资 // 别名
SELECT AVG(sal), deptno
FROM emp GROUP BY deptno
HAVING AVG(sal) < 2000;
分页查询limit
start类似于数组的下标记发
rows 代表所取的行的数量
从start+1行开始取取出rows行
SELECT *FROM emp
ORDER BY empno
LIMIT 0,3 --查询前三行
边栏推荐
- High speed layout guidelines incomplete
- leetcode 674 最长递增子串
- A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
- Stream flow precautions
- js两数之和
- Error record: illegalstateexception: optional int parameter 'XXXX' is
- Office application cannot start normally 0xc0000142
- vant3+ts 封装uploader上传图片组件
- Resttemplateconfig configuration print request response log under soringboot
- Random talk about redis source code 90
猜你喜欢
HTTP cache < strong cache and negotiation cache >
轻量、便捷的小程序转App技术方案,实现与微信/流量App互联互通
Nixos 22.05 installation process record
C#简单介绍
Write a select based concurrent server
Queue priority of message queue practice
SSM integrates FreeMarker and common syntax
Comparison of disk mapping tools for network disk and object cloud storage management
1.5 what is an architect (serialization)
干货 | 一文搞定 pytest 自动化测试框架(二)
随机推荐
在同花顺开户证券安全吗
Esp-idf adds its own components
es7不使用父子和嵌套关系来实现一对多功能
Relationship between resolution and line field synchronization signal
Typescript type declaration file (III)
PHP implementation of infinite classification tree (recursion and Optimization)
Use applet to quickly generate app in seven steps
DRM driven MMAP detailed explanation: (I) preliminary knowledge
Introduction to cookie usage
静态内存分配和动态内存分配小结
An easy-to-use IDE for small programs
General differences between SQL server versions released by Microsoft in different periods so far, for reference
有源差分晶振原理图以及LV-PECL、LVDS、HCSL区别
Gossip about the 88 of redis source code
High speed layout guidelines incomplete
js快速排序
网盘和对象云存储管理之磁盘映射工具比较
GD32F4xx控制DGUS触控按键
面试题总结
Stream流注意点