為你的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

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”.

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM