当前位置:网站首页>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 (3)
- What is the most important ability of a programmer?
- The virtual reality real estate display system foresees the future decoration effect in advance
- Nacos database configuration
- go里面的基本知识
- DNS resolution process
- MySQL高级语句(一)
- npm、cnpm的安装
- Node installation and environment variable configuration
- 物联网如何改变城市运行效率
猜你喜欢

Not annotated parameter overrides @NonNullApi parameter

点云旋转到参考坐标系方向(最小方向包围盒方法)

Node installation and configuration (node-v12.20.2-x64 ) and introduction to node version switching

The installation of NPM, CNPM

pytorch basic operations: classification tasks using neural networks

MySQL高级语句(一)

代码编世界 科技创未来

有点奇怪!访问目的网址,主机能容器却不行

MySQL经典50道练习题及全网最详细解析

Ant three sides: MQ message loss, duplication, backlog problem, what are the solutions?
随机推荐
APP special test: traffic test
Not annotated parameter overrides @NonNullApi parameter
Guarantee WIFI security in home and enterprise-with AC and AP networking experiment
How to install the specified version package with NPM and view the version number
Py之mlxtend:mlxtend库的简介、安装、使用方法之详细攻略
MySql -- 不存在则插入,存在则更新或忽略
Launch Space on-premises deployment (local) Beta!
蚂蚁三面:MQ 消息丢失、重复、积压问题,有哪些解决方案?
MarkDown公式指导手册
洛谷小游戏大全(用洛谷的人都得知道)
MySQL 5.7 安装教程(全步骤、保姆级教程)
What is the most important ability of a programmer?
MySQL(3)
BGP experiment (route reflector, federation, route optimization)
Important concepts of target detection - IOU, receptive field, hole convolution, mAP
科技赋能拉萨之“肺”,华为助力拉鲁湿地智慧管理守护绿水青山
Xgboost报错ValueError:无效的形状:标签(1650 2)
VMTK环境配置记录
MySQL Index Common Interview Questions (2022 Edition)
MySQL Advanced Study Notes