为你的Mysql排序查询增加一个排序号
排序号,在需要排序的查询中比较常见,今天再一次遇到这种场景,不常写,所以上手比较生疏,记录一下,或许对更多的人也有用处。
起初在网上进行了一下简单的搜索,但是文章都挺乱,可读性都不太高,经过一番调查,结合官网文档对此类场景做如下描述:
- 使用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
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
SETstatement. 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 inSETstatements) 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;
-> 4You can make value assignments using
:=in other statements besidesSELECT, such asUPDATE, 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, 7While 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
SETstatement,=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 aSETstatement,=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 sameSETstatement.In the
SETclause of anUPDATEstatement,=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 anyWHEREconditions that are part of theUPDATEare met. You can make multiple assignments in the sameSETclause of anUPDATEstatement.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, 1For 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排序查询增加一个排序号的更多相关文章
- MYSQL进阶学习笔记十:MySQL慢查询!(视频序号:进阶_23-24)
知识点十一:MySQL 慢查询的应用(23) 一.慢查询定义 MySQL记录下查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为慢查询. 查看时间限制 show variables lik ...
- MySQL排序查询
语法:① SELECT 查询 (要找的东西)②FROM 表 (在哪个表找)③[WHERE 筛选条件](取出重要的或有用的)④ORDER BY 排序列表 [ASC|DESC] (排序的关键字 字段)([ ...
- 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 ...
- MySQL 为日期增加一个时间间隔
set @dt = now(); select date_add(@dt, interval 1 day); - 加1天 select date_add(@dt, interval 1 hour) ...
- mysql给查询的结果添加序号
1.法一: select (@i:=@i+1) i,a.url from base_api_resources a ,(select @i:=0) t2 order by a.id de ...
- mysql中查询一个字段属于哪一个数据库中的哪一个表的方式
mysql中查询一个字段具体是属于哪一个数据库的那一张表:用这条语句就能查询出来,其中 table_schema 是所在库, table_name 是所在表 --mysql中查询某一个字段名属于哪一个 ...
- 分享一个mysql 复杂查询的例子
发布:脚本学堂/MySQL 编辑:thebaby 2013-08-23 09:37:37 [大 中 小] 有关mysql复杂查询的一个例子,正在学习mysql的朋友,可以作为一个参考. 在my ...
- MySQL慢查询(二) - pt-query-digest详解慢查询日志
一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...
- mysql慢查询----pt-query-digest详解慢查询日志(linux系统)
一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...
随机推荐
- 牛客网 湖南大学2018年第十四届程序设计竞赛重现赛 A game
链接:https://www.nowcoder.com/acm/contest/125/A来源:牛客网 Tony and Macle are good friends. One day they jo ...
- JavaScript中的Cookie 和 Json的使用
JavaScript中的Cookie 和 Json的使用 JSON JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.采用的是完全独立于编程语言的文本格式来存 ...
- 【Offer】[14] 【剪绳子】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 给你一根长度为n绳子,请把绳子剪成m段(m.n都是整数,n>1并且m≥1).每段的绳子的长度记为k[0].k[1].--.k[m] ...
- centos 6.8 下没有yum命令解决方法(报错: -bash: yum: command not found)
1.去 http://mirrors.163.com/centos/6/os/x86_64/Packages/ 地址下载4个rpm安装包:python-iniparse-0.3.1-2.1.el6.n ...
- Lottie在手,动画我有:ios/Android/Web三端复杂帧动画解决方案
为什么需要Lottie 在相对复杂的移动端应用中,我们可能会需要使用到复杂的帧动画.例如: 刚进入APP时候可能会看到的入场小动画,带来愉悦的视觉享受 许多Icon的互动变化比较复杂多变的时候,研 ...
- Hive介绍和安装部署
搭建环境 部署节点操作系统为CentOS,防火墙和SElinux禁用,创建了一个shiyanlou用户并在系统根目录下创建/app目录,用于存放 Hadoop等组件运行包.因为该目录用于安装h ...
- 学习数据库SQL语句1
数据库一直让我很头大,正好出差有空,就重新恶补起来吧!(网站:http://www.w3school.com.cn/sql) 我准备把我每天学到的都记录下来=.= (红色字体代表关键词,蓝色字体是我个 ...
- C#将数据导入到excel文件
最近在做C#对excel的操作程序,简单的与datagridview的交互如下 using System;using System.Collections.Generic;using System.C ...
- RDDs基本操作之Transformations
逐元素Transformation map() map()接收函数,把函数应用到RDD的每个元素,返回新的RDD 举例: val lines = sc.parallelize(Array(" ...
- javascript数组/对象数组的深浅拷贝问题
一.问题描述 在项目里的一个报名页面需要勾选两条信息(信息一和信息二),由于信息一和信息二所拥有的数据是一致的,所以后台只返回了一个对象数组,然后在前台设置了两个List数组来接收并加以区分.原型如下 ...