MySQL-高级语句
文章目录
- 一、SQL高级语句
- 1. select
- 2. distinct
- 3. where
- 4. and,or
- 5. in
- 6. between
- 7. like 通配符
- 8. order by
- 9. 函数
- 9.1 数学函数
- 9.2 聚合函数
- 10. group by
- 11. having
- 12. 别名
- 13. 子查询
- 14. EXISTS
一、SQL高级语句
1. select
显示表格中的一个或者多个字段中所有的信息
#格式:select 字段名 from 表名;select * from students;
select name,score from students;
2. distinct
查询不重复记录
#格式:
select distinct 字段 from 表名﹔
select distinct age from students;
select distinct gender from students;
3. where
where 有条件的查询
#格式:
select '字段' from 表名 where 条件
select name,age from students where age<15;
4. and,or
and: 且 , or: 或
#格式:
select 字段名 from 表名 where 条件1 (and|or) 条件2 (and|or)条件3;
select name,age from students where age>15 and age<20;
5. in
显示已知值的资料
#格式:
select 字段名 from 表名 where 字段 in ('值1','值2'....);
select * from students where id in (1,2,3,4);
select * from students where age in (10,17);
6. between
显示两个值范围内的资料
#格式:
select 字段名 from 表名 where 字段 between '值1' and '值2';
包括 and两边的值
select * from students where id between 1 and 5;select * from students where id between 1 and 4;
select * from students where score between 10 and 50;
select * from students where name between 'haijiu' and 'wangwu';
select * from students where age between 10 and 15;
7. like 通配符
通配符通常是和 like 一起使用
#格式:
select 字段名 from 表名 where 字段 like 通配符格式
通配符 | 含义 |
---|---|
% | 表示零个,一个或者多个字符 |
_ | 下划线表示单个字符 |
A_Z | 所有以A开头 Z 结尾的字符串 ‘ABZ’ ‘ACZ’ 'ACCCCZ’不在范围内 下划线只表示一个字符 AZ 包含a空格z |
ABC% | 所有以ABC开头的字符串 ABCD ABCABC |
%CBA | 所有以CBA结尾的字符串 WCBA CBACBA |
%AN% | 所有包含AN的字符串 los angeles |
_AN% | 所有 第二个字母为 A 第三个字母 为N 的字符串 |
select * from students where name like 'h%';
select * from students where name like '_a%';
select * from students where name like '%liu%';
8. order by
order by 按关键字排序
#语法:
select 字段名 from 表名 where 条件 order by 字段 [asc,desc];
asc :正向排序
desc :反向排序
select id,name from students order by id;
select id,name from students order by id desc;
select id,name,age from students where age=17 order by id;
9. 函数
9.1 数学函数
函数 | 含义 |
---|---|
abs(x) | 返回x 的 绝对值 |
rand() | 返回0到1的随机数 |
mod(x,y) | 返回x除以y以后的余数 |
power(x,y) | 返回x的y次方 |
round(x) | 返回离x最近的整数 |
round(x,y) | 保留x的y位小数四舍五入后的值 |
sqrt(x) | 返回x的平方根 |
truncate(x,y) | 返回数字 x 截断为 y 位小数的值 |
ceil(x) | 返回大于或等于 x 的最小整数 |
floor(x) | 返回小于或等于 x 的最大整数 |
greatest(x1,x2…) | 返回返回集合中最大的值 |
least(x1,x2…) | 返回返回集合中最小的值 |
select abs(-6);
select rand(1);
select * from xues order by rand();
select mod(10,3);
select power(2,10);
select round(6.5);select round(6.4);
select round(3.1415926,2);
select truncate(3.1415,3);select truncate(3.1455,3);
select ceil(3.1415);
select floor(3.1415);
select greatest(12,13,14,15);
select least(12,13,14,15);
9.2 聚合函数
函数 | 含义 |
---|---|
avg() | 返回指定列的平均值 |
count() | 返回指定列中非 NULL 值的个数 |
min() | 返回指定列的最小值 |
max() | 返回指定列的最大值 |
sum(x) | 返回指定列的所有值之和 |
select avg(age) from xues;select avg(score) from xues;
select sum(age) from xues;
select max(age) from xues;
select min(age) from xues;
select count(classid) from xues;count(明确字段):不会忽略空记录
select count(*) from xues;count(*)包含空字段,会忽略空记录
查看空格字段是否会被匹配
insert into xues values(16,'',20,82,'ppp',1);
10. group by
对group by 后面的字段的查询结果进行汇总分组,通常是结合聚合函数一起使用的
group by 有一个原则,就是select 后面的所有列中,没有使用聚合函数的列必须出现在 group by 的后面。
#格式:
select 字段1,sum(字段2) from 表名 group by 字段1;
select classid,sum(age) from xues group by classid;
select classid,avg(age) from xues group by classid;
select classid,count(age) from xues group by classid;
11. having
having:用来过滤由group by语句返回的记录集,通常与group by语句联合使用
having语句的存在弥补了where关键字不能与聚合函数联合使用的不足。如果被SELECT的只有函数栏,那就不需要GROUP BY子句。
要根据新表中的字段,来指定条件
#格式:
SELECT 字段1,SUM("字段")FROM 表格名 GROUP BY 字段1 having(函数条件);
select classid,avg(score) from xues group by classid;select classid,avg(score) from xues group by classid having avg(score)>80;
12. 别名
栏位別名 表格別名
#格式:
SELECT "表格別名"."栏位1" [AS] "栏位別名" FROM "表格名" [AS] "表格別名";
select f.classid,avg(score) '平均成绩' from xues as f group by f.classid;
13. 子查询
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL语句
#格式:
SELECT "栏位1" FROM "表格" WHERE "栏位2" [比较运算符]
#子查询
(SELECT "栏位1" FROM "表格" WHERE "条件");
select sum(score) from xues where id in (select id from students where id=1);
14. EXISTS
用来测试内查询有没有产生任何结果类似布尔值是否为真
如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。
#格式:
SELECT "栏位1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
先看students表中是否有id为1的学生,如果有则执行将xues表中的score求和select sum(score) from xues where exists (select * from students where id=1);
#示例2:先看students表中是否有id为16的学生,如果有则执行将xues表中的score求和select sum(score) from xues where exists (select * from students where id=16);
MySQL-高级语句相关推荐
- MySQL高级语句简单介绍
文章目录 一.常用查询 1.按关键字排序 1.1 按单字段排序 1.2 条件查询 1.3 多字段排序 1.4 区间判断 1.5 查询不重复记录 2.对结果进行分组 3.限制结果条目 4.设置别名(al ...
- MySQL高级语句精讲
索引目录 一.按关键字排序 1.单字段排序 2.多字段排序 3.对结果进行排序 二.对结果进行分组 1.group by分组 2.group by结合order by 三.限制结果条目 LIMIT语法 ...
- MySQL存储过程分支语句_mysql 高级语句--存储过程(PROCEDURE)
mysql 高级语句 一.存储过程 1.什么是存储过程: 就是一组SQL语句集,功能强大,可以实现一些比较复杂的逻辑功能. ps:存储过程跟触发器有点类似,都是一组SQL集,但是存储过程是主动调用的, ...
- linux mysql select_MySQL-Select语句高级应用
阅读目录 1.1 SELECT高级应用 1.2 select中where子句使用 1.3 select中ORDER BY子句 1.4 LIMIT子句 1.5 多表连接查询 1.6 Informatic ...
- MySQL高级查询语句——超详细,一篇就够了
MySQL高级查询语句 一.MySQL进阶查询 1.1.按关键字排序--order by 1.2.对结果进行分组--group by 1.2.1.单个分组 1.2.2.分组结合order by使用 1 ...
- Mysql高级查询语句练习
Mysql高级查询语句练习 DROP TABLE IF EXISTS `tblcourse`; CREATE TABLE `tblcourse` ( `CourseId` varchar(3) NOT ...
- MysQL高级SQL语句|进阶语句|select语句(一)【图文细解】
MysQL高级SQL语句 一.SQL语句的概述 SELECT DISTINCT WHERE AND.OR IN BETWEEN 通配符 LIKE ORDER BY GROUP BY HAVING 二. ...
- MySQL高级SQL语句(终章)
目录 一:通配符 1.1查询名字是有开头的记录 1.2查询名字里是贝和摩中间有一个字符的记录 1.3查看贝后面3个字符的名字记录 1.4通配符%和_结合使用 二:子查询 2.1相同表查询 2.2不同表 ...
- 数据库应用:MySQL数据库SQL高级语句与操作
目录 一.理论 1.克隆表与清空表 2.SQL高级语句 3.SQL函数 4.SQL高级操作 5.MySQL中6种常见的约束 二.实验 1.克隆表与清空表 2.SQL高级语句 3.SQL函数 4.SQL ...
- 【MySQL数据库】MySQL 高级SQL 语句一
@[TOC](MySQL 高级SQL 语句 一.MySQL 高级SQL 语句 1.1select -显示表格中一个或数个字段的所有数据记录 1.2distinct不显示重复的数据记录 1.3where ...
最新文章
- Linux下的权限掩码umask
- SringBoot项目使用maven打包,使用docker部署,并且使用thymeleaf作为模板引擎遇到的问题
- ICCV 2019 | 通过多标签相关性研究提升神经网络视频分类能力
- 服务端监控要怎么做?
- HDU 3916 Sequence Decomposition 【贪心】
- 如何安全地迁移到Exchange 2003?
- Seesion工作原理
- mac安装gcc10
- 智能优化算法:静电放电算法-附代码
- 三年的php简历_php应聘个人简历模板
- 网络安全术语基础知识
- 马云留给阿里的财富:4个应用场景落地+90个区块链专利
- php聚合支付,pay: ThinkPHP开源聚合支付系统
- python的power,Python numpy.power()函数使用说明
- 前端常用PS技巧总结之将图片上的LOGO(水印)去掉
- 信庭嵌入式工作室-ARM应用技术之体系结构应用(中)
- 东大22春《马克思主义基本原理概论》在线平时作业1百分非答案
- 无盘服务器4根网线雨两根网线的区别,网线接法的描述:实际用到4根。
- 【学习笔记】彻底删除2345输入法
- leetcode/求平方根
热门文章
- linux vim插件
- nba2k18服务器暂时不可用,NBA2K18连不上服务器解决方法 连接不上服务器咋办_3DM单机...
- plantuml 依赖_plantuml语法
- BACnet协议详解——序
- 【PCL】LINK2001 “protected:virtual void_cdel pcl::XXX报错
- 【渝粤教育】国家开放大学2019年春季 773初级会计电算化 参考试题
- Android通过蓝牙获取通讯录
- 爬虫第三弹:图灵社区书籍信息爬取
- 【鱼C·零基础入门学习Python】课后习题答案 第14课
- 关于一般局域网如何应对ARP***