当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 如何优化OpenSumi终端性能?
- Nacos注册中心的部署与用法详细介绍
- [OpenCV from entry to practice] image processing technology [pixel] (the most detailed in the whole network)
- Kingdee International: Lost in half a year and last year, how does the business model of frantically burning money continue
- Guarantee WIFI security in home and enterprise-with AC and AP networking experiment
- rhce作业
- 排雷小游戏(C语言)
- 跨桌面端Web容器演进
- leetcode一步解决链表反转问题
- Constructors, member variables, local variables
猜你喜欢
![[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays](/img/eb/9b05508e88b7f17d80de2afa8c08ce.png)
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays

Write implicit join on view with jOOQ 3.14 synthetic foreign key

C语言基础知识梳理总结:零基础入门请看这一篇

BGP+MPLS Comprehensive Experiment

MySQL联合查询(多表查询)

触发器简单解释

zabbix邮件报警和微信报警

Nacos数据库配置

Double for loop case (use js jiujiu printing multiplication table)
![[Cartoon] 2021 full score programmer behavior comparison table (latest version)](/img/18/2679b35f39b909aa6e013ddf01114b.png)
[Cartoon] 2021 full score programmer behavior comparison table (latest version)
随机推荐
Cyber Security Learning - Intranet Penetration 4
Shell 脚本不同玩法
跨桌面端Web容器演进
【漫画】2021满分程序员行为对照表(最新版)
Common functions of pytorch
Difference between npm and yarn
zabbix邮件报警和微信报警
Analysis of port 9848 error at startup of Nacos client (non-version upgrade problem)
物联网如何改变城市运行效率
selenium + robotframework的运行原理
深度学习——CNN实现MNIST手写数字的识别
虚拟现实房产展示系统提前预见未来装修效果
MySQL索引常见面试题(2022版)
目标检测重要概念——IOU、感受野、空洞卷积、mAP
Write implicit join on view with jOOQ 3.14 synthetic foreign key
反向解析dns服务器
使用TinkerPop框架对GDB增删改查
Introduction to coredns
DNS的解析流程
Nacos数据库配置