当前位置:网站首页>MySQL高级语句(一)
MySQL高级语句(一)
2022-08-02 05:19: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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- The installation of NPM, CNPM
- flex layout (flexible layout)
- Block elements, inline elements (elements, span elements)
- C 竞赛——捕鱼
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- Deep learning - CNN realizes the recognition of MNIST handwritten digits
- Nacos安装详细过程
- Stress testing and performance analysis of node projects
- 51 MCU peripherals: DS18B20
- C竞赛训练
猜你喜欢

程序员最重要的能力是什么?

go里面的基本知识

HCIP第十七天

About the directory structure of the web application

股价屡创新低 地产SaaS巨头陷入困境 明源云该如何转型自救?

The installation of NPM, CNPM

Guarantee WIFI security in home and enterprise-with AC and AP networking experiment

zabbix邮件报警和微信报警

Shuttle + Alluxio 加速内存Shuffle起飞

What is the most important ability of a programmer?
随机推荐
程序员写PPT的小技巧
MySQL database video tutorial from beginner to proficient
Leetcode parentheses matching problem -- 32. The longest parentheses effectively
51 MCU peripherals: DS18B20
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
C语言操作符详解(2)
科技赋能拉萨之“肺”,华为助力拉鲁湿地智慧管理守护绿水青山
触发器简单解释
Kingdee International: Lost in half a year and last year, how does the business model of frantically burning money continue
Introduction to coredns
Polar Parametrization for Vision-based Surround-View 3D Detection 论文笔记
秒杀系统小demo
跨桌面端Web容器演进
洛谷小游戏大全(用洛谷的人都得知道)
PIL与numpy格式之间的转换
[Cartoon] 2021 full score programmer behavior comparison table (latest version)
Stress testing and performance analysis of node projects
leetcode-338.比特位计数
Practice on optimizing startup performance of VS Code
How to install the specified version package with NPM and view the version number