“ORDER BY ...,LEAST(0,@ rownum:= @ rownum + 1)”如何工作?

时间:2018-04-09 12:10:44

标签: mysql

我现在正致力于高性能MYSQL,第三版。在6.7.9节中,我运行语句A:

# SQL statement A
mysql> SET @rownum := 0;
mysql> SELECT actor_id, first_name, @rownum AS rownum
    -> FROM sakila.actor
    -> WHERE @rownum <= 1
    -> ORDER BY first_name, LEAST(0, @rownum := @rownum + 1);
+----------+------------+--------+
| actor_id | first_name | rownum |
+----------+------------+--------+
|        2 | NICK       |      2 |
|        1 | PENELOPE   |      1 |
+----------+------------+--------+

select @rownum;
+---------+
| @rownum |
+---------+
|       2 |
+---------+  

select * from sakila.actor limit 10;
+----------+------------+--------------+---------------------+
| actor_id | first_name | last_name    | last_update         |
+----------+------------+--------------+---------------------+
|        1 | PENELOPE   | GUINESS      | 2006-02-15 04:34:33 |
|        2 | NICK       | WAHLBERG     | 2006-02-15 04:34:33 |
|        3 | ED         | CHASE        | 2006-02-15 04:34:33 |
|        4 | JENNIFER   | DAVIS        | 2006-02-15 04:34:33 |
|        5 | JOHNNY     | LOLLOBRIGIDA | 2006-02-15 04:34:33 |
|        6 | BETTE      | NICHOLSON    | 2006-02-15 04:34:33 |
|        7 | GRACE      | MOSTEL       | 2006-02-15 04:34:33 |
|        8 | MATTHEW    | JOHANSSON    | 2006-02-15 04:34:33 |
|        9 | JOE        | SWANK        | 2006-02-15 04:34:33 |
|       10 | CHRISTIAN  | GABLE        | 2006-02-15 04:34:33 |
+----------+------------+--------------+---------------------+

似乎前两行已经排序并返回。任何人都可以解释上述声明的运行顺序?

0 个答案:

没有答案