当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Cyber Security Learning - Intranet Penetration 4
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
- MarkDown公式指导手册
- leetcode一步解决链表合并问题
- C语言操作符详解(2)
- npm does not recognize the "npm" item as the name of a cmdlet, function, script file, or runnable program.Please check the spelling of the name, and if the path is included, make sure the path is corr
- Home NAS server (4) | MergerFS and SnapRaid data backup
- 如何优化OpenSumi终端性能?
- Write implicit join on view with jOOQ 3.14 synthetic foreign key
猜你喜欢

There are more and more talents in software testing. Why are people still reluctant to take the road of software testing?

国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐

leetcode一步解决链表合并问题

The virtual reality real estate display system foresees the future decoration effect in advance

25K test old bird's 6-year experience in interviews, four types of companies, four types of questions...

触发器简单解释

腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践

关于 VS Code 优化启动性能的实践

leetcode每天5题-Day04

MarkDown Formula Instruction Manual
随机推荐
Shuttle + Alluxio 加速内存Shuffle起飞
HCIP BGP Comprehensive Experiment Establishing peers, route reflectors, federation, route announcement and aggregation
Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
Important concepts of target detection - IOU, receptive field, hole convolution, mAP
如何优化OpenSumi终端性能?
双重for循环案例(用js打印九九乘法表)
引领需求 为HR价值正名——“人力资源领先模型HRLM”成功首发
51 MCU peripherals: ADC
Contents of encoding-indexes.js file printed with Bluetooth:
保证家里和企业中的WIFI安全-附AC与AP组网实验
View source and switch mirrors in two ways: npm and nrm
C 竞赛——捕鱼
OAuth 授权协议 | 都云原生时代了,我们应该多懂一点OAuth ?
C# 编码规范手册
[OpenCV from entry to practice] image processing technology [pixel] (the most detailed in the whole network)
APT + Transform 实现多模块应用Application生命周期分发
What is the most important ability of a programmer?
使用jOOQ 3.14合成外键在视图上写隐式连接
nacos registry
Common functions of pytorch