当前位置:网站首页>MySQL Advanced Statements (1)
MySQL Advanced Statements (1)
2022-08-02 06:57:00 【m0_67402013】
目录
一、按关键字排序
1.1 Order By 语句
order by语句可以去进行数据的排序,一般后面跟字段.
语法:select 字段1,字段2 from 表名 order by 字段;
1.2 升序排序
select * from id,name from ceshi order by id;

1.3 降序排列
select id,name from ceshi order by id desc;

1.4 条件查询
select id,name from ceshi where id>2 order by id;

二、区间查询和去重查询
2.1 AND和OR
select 字段名 from 表名 where 条件1 (and|or) 条件2 (and|or)条件3;
2.1.1 and(且)
select * from 表名 where 条件1 and 条件2;

2.1.2 or(或者)
select * from 表名 where 条件1 or条件2;

2.1.3 嵌套
select * from 表名 where 条件1 or 条件2 (条件3 and 条件4);
有括号会先执行括号里的内容然后再去执行括号外的

2.2 查询不重复的 distinct
select distinct 字段 from 表名;

三、对结果进行分组
通过 SQL 查询出来的结果,还可以对其进行分组,使用 GROUP BY 语句来实现 ,GROUP BY 通常都是结合聚合函数一起使用的,常用的聚合函数包括:计数(COUNT)、 求和(SUM)、求平均数(AVG)、最大值(MAX)、最小值(MIN),GROUP BY 分组的时候可以按一个或多个字段对结果进行分组处理.
select conut(字段) from 表名 group by 字段;

使用条件去统计

四、限制结果
4.1 limit 语法
LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示. 如果不设定第一个参数,将会从表中的第一条记录开始显示.
需要注意的是,第一条记录的 位置偏移量是 0,第二条是 1,以此类推.第二个参数是设置返回记录行的最大数目.
4.1.1 查询前四行
select * from 表名 limit 4;

4.1.2 从第二行开始查询后3行

五、设置别名
在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者 多次使用相同的表,可以给字段列或表设置别名.使用的时候直接使用别名,简洁明了,增强可读性(alias ——》as)
5.1 as语法
5.1.1 列别名设置
select 字段名 as 你想要设置的别名 from 表名;

如果表的名字比较长可以用as设置别名
select 表的别名.字段 as 字段的别名 from 表名 as 设置的表的别名;

5.2 查询表的字段数量以number显示出
select count(*) as number from 表名

5.3 as将查询到的数据导入另一个表中
select table 新表名 as select * from 表名;

总结
本章介绍了select 的高阶语句
order by 降序升序排序
and和or的使用
distinct的去重查询
limit 限制输出
as的别名设置
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- MySQL 23 classic interviews hang the interviewer
- Different ways of shell scripting
- 目标检测重要概念——IOU、感受野、空洞卷积、mAP
- Node installation and configuration of environment variables
- Technology empowers Lhasa's "lungs", Huawei helps Lalu Wetland Smart Management to protect lucid waters and lush mountains
- MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
- nodejs的安装和全局配置(超详细哦)
- BGP实验(路由反射器,联邦,路由优化)
- Not annotated parameter overrides @NonNullApi parameter
- oracle 远程连接数据库
猜你喜欢

Launch Space on-premises deployment (local) Beta!

MySQL 5.7 安装教程(全步骤、保姆级教程)

Analysis of the source code of the JS UI framework of Hongmeng system

直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践

Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"

DNS的解析流程

MarkDown公式指导手册

制作web3d动态产品展示的优点

npm、cnpm的安装

Nacos installation detailed process
随机推荐
The international top conference OSDI included Taobao system papers for the first time, and the device-cloud collaborative intelligence was recommended by the keynote speech of the conference
MySql统计函数COUNT详解
物联网如何改变城市运行效率
Node的安装与环境变量的配置
go里面的基本知识
Nodejs安装教程
MySQL Advanced SQL Statements
nacos安装配置和单机部署教程
Nacos database configuration
Write implicit join on view with jOOQ 3.14 synthetic foreign key
MarkDown Formula Instruction Manual
NPM ---- 安装yarn
An advanced method for solving palindromes
leetcode-318.最大单词长度乘积
Difference between npm and yarn
线程基础(一)
mysql索引失效的常见9种原因详解
Node installation and configuration of environment variables
触发器简单解释
股价屡创新低 地产SaaS巨头陷入困境 明源云该如何转型自救?