排序号,在需要排序的查询中比较常见,今天再一次遇到这种场景,不常写,所以上手比较生疏,记录一下,或许对更多的人也有用处。

起初在网上进行了一下简单的搜索,但是文章都挺乱,可读性都不太高,经过一番调查,结合官网文档对此类场景做如下描述:

  • 使用mysql变量定义语法
  • 每一行对定义好的变量进行+1
  • 同一个sql中如果出现union,或者子查询,变量名称需要不同。

示例代码:

select
@rank_8 := @rank_8 + 1 AS rank_no,
...
from a,(SELECT @rank_8 := 0) b
where ...
order by xxx desc limit 15

代码说明:

  • 将变量定义使用一个简单的子查询b
  • b会优先定义该变量,然后就可以在外层查询中进行引用操作了。

附上官网变量使用文档:

12.3.4 Assignment Operators

Table 12.5 Assignment Operators

Name Description
= Assign a value (as part of a SET statement, or as part of the SET clause in an UPDATE statement)
:= Assign a value
  • :=

    Assignment operator. Causes the user variable on the left hand side of the operator to take on the value to its right. The value on the right hand side may be a literal value, another variable storing a value, or any legal expression that yields a scalar value, including the result of a query (provided that this value is a scalar value). You can perform multiple assignments in the same SET statement. You can perform multiple assignments in the same statement.

    Unlike =, the := operator is never interpreted as a comparison operator. This means you can use := in any valid SQL statement (not just in SET statements) to assign a value to a variable.

    mysql> SELECT @var1, @var2;
    -> NULL, NULL
    mysql> SELECT @var1 := 1, @var2;
    -> 1, NULL
    mysql> SELECT @var1, @var2;
    -> 1, NULL
    mysql> SELECT @var1, @var2 := @var1;
    -> 1, 1
    mysql> SELECT @var1, @var2;
    -> 1, 1 mysql> SELECT @var1:=COUNT(*) FROM t1;
    -> 4
    mysql> SELECT @var1;
    -> 4

    You can make value assignments using := in other statements besides SELECT, such as UPDATE, as shown here:

    mysql> SELECT @var1;
    -> 4
    mysql> SELECT * FROM t1;
    -> 1, 3, 5, 7 mysql> UPDATE t1 SET c1 = 2 WHERE c1 = @var1:= 1;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT @var1;
    -> 1
    mysql> SELECT * FROM t1;
    -> 2, 3, 5, 7

    While it is also possible both to set and to read the value of the same variable in a single SQL statement using the:= operator, this is not recommended. Section 9.4, “User-Defined Variables”, explains why you should avoid doing this.

  • =

    This operator is used to perform value assignments in two cases, described in the next two paragraphs.

    Within a SET statement, = is treated as an assignment operator that causes the user variable on the left hand side of the operator to take on the value to its right. (In other words, when used in a SET statement, = is treated identically to :=.) The value on the right hand side may be a literal value, another variable storing a value, or any legal expression that yields a scalar value, including the result of a query (provided that this value is a scalar value). You can perform multiple assignments in the same SET statement.

    In the SET clause of an UPDATE statement, = also acts as an assignment operator; in this case, however, it causes the column named on the left hand side of the operator to assume the value given to the right, provided any WHERE conditions that are part of the UPDATE are met. You can make multiple assignments in the same SET clause of an UPDATE statement.

    In any other context, = is treated as a comparison operator.

    mysql> SELECT @var1, @var2;
    -> NULL, NULL
    mysql> SELECT @var1 := 1, @var2;
    -> 1, NULL
    mysql> SELECT @var1, @var2;
    -> 1, NULL
    mysql> SELECT @var1, @var2 := @var1;
    -> 1, 1
    mysql> SELECT @var1, @var2;
    -> 1, 1

    For more information, see Section 13.7.4.1, “SET Syntax for Variable Assignment”Section 13.2.11, “UPDATE Syntax”, and Section 13.2.10, “Subquery Syntax”.

为你的Mysql排序查询增加一个排序号的更多相关文章

  1. MYSQL进阶学习笔记十:MySQL慢查询!(视频序号:进阶_23-24)

    知识点十一:MySQL 慢查询的应用(23) 一.慢查询定义 MySQL记录下查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为慢查询. 查看时间限制 show variables lik ...

  2. MySQL排序查询

    语法:① SELECT 查询 (要找的东西)②FROM 表 (在哪个表找)③[WHERE 筛选条件](取出重要的或有用的)④ORDER BY 排序列表 [ASC|DESC] (排序的关键字 字段)([ ...

  3. union排序,起别名将两个不同的字段ZCDZ,SCJYDZ 变成同一个别名dz,进行排序;增加一个字段z,进行排序。

    with a as( select NSRSBH,NSRMC,ZGSWJ_DM,ZGSWSKFJ_DM,SSGLY_DM,FDDBRXM,ZCDZ dz,1 z from hx_dj.dj_nsrxx ...

  4. MySQL 为日期增加一个时间间隔

    set @dt = now(); select date_add(@dt, interval 1 day);   - 加1天 select date_add(@dt, interval 1 hour) ...

  5. mysql给查询的结果添加序号

    1.法一: select  (@i:=@i+1)  i,a.url from  base_api_resources a  ,(select   @i:=0)  t2 order by a.id de ...

  6. mysql中查询一个字段属于哪一个数据库中的哪一个表的方式

    mysql中查询一个字段具体是属于哪一个数据库的那一张表:用这条语句就能查询出来,其中 table_schema 是所在库, table_name 是所在表 --mysql中查询某一个字段名属于哪一个 ...

  7. 分享一个mysql 复杂查询的例子

    发布:脚本学堂/MySQL  编辑:thebaby   2013-08-23 09:37:37  [大 中 小] 有关mysql复杂查询的一个例子,正在学习mysql的朋友,可以作为一个参考. 在my ...

  8. MySQL慢查询(二) - pt-query-digest详解慢查询日志

    一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...

  9. mysql慢查询----pt-query-digest详解慢查询日志(linux系统)

    一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...

随机推荐

  1. 现代 JavaScript 教程到底是什么?

    手册与规范 <现代 JavaScript 教程>是开源的现代 JavaScript 从入门到进阶的优质教程,它旨在帮助你逐渐掌握 JavaScript 这门语言.但是一旦你已经熟悉了这门语 ...

  2. 【Offer】[5] 【替换空格】

    题目描述 思路分析 Java代码 代码链接 题目描述 请实现一个函数,把字符串中的每个空格替换成"%20". 例如输入"We are happy.",则输出&q ...

  3. MyBatis中#{}和${}的区别详解

    首先看一下下面两个sql语句的区别: <select id="selectByNameAndPassword" parameterType="java.util.M ...

  4. HTML制作WORD表格

    效果图 table属性 align:让内容居中,取值为left.right.center border:设置表格的边框 cellspacing:单元格与单元格之间的距离 bgcolor:表格的背景色 ...

  5. Java深拷贝与序列化

    对基本类型的变量进行拷贝非常简单,直接赋值给另外一个对象即可: int b = 50; int a = b; // 基本类型赋值 对于引用类型的变量(例如 String),情况稍微复杂一些,因为直接等 ...

  6. TypeScript && React

    环境搭建 我们当然可以先用脚手架搭建React项目,然后手动配置成支持TypeScript的环境,虽然比较麻烦,但可以让你更清楚整个过程.这里比较麻烦,就不演示了,直接用命令配置好. npx crea ...

  7. 【原创】(四)Linux内存模型之Sparse Memory Model

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  8. String、StringBuffer、StringBulider的区别

    1.线程安全性: 线程安全:String.StringBuffer 线程不安全:StringBulider 2.执行效率 StringBulider最快,Stringbuffer次之,String最差 ...

  9. 上手Dubbo之 环境搭建

    和传统ssm整合--写XML配置文件 搭建服务的提供者和服务的消费者,实现服务消费者跨应用远程调用服务提供者 公共模块抽取 公共模块的抽取 服务的消费者远程调用服务的提供者, 最起码他自己要得到在服务 ...

  10. android studio连接雷电模拟器 【AS 模拟器】

    ⭐ 我的网站: www.mengyingjie.com ⭐ Android studio 怎么连接安卓模拟器呢,跟着下面的步骤即可 (以雷电模拟器为例,其他模拟器类似) 一.下载模拟器 下载安装完雷电 ...