Logic Value of Assignment Expression I'm doing the leetcode SQL problems and kind of confused with the code below:

select distinct Num from (
    select
        Num,
        case
            when @prevNum = Num then @count := @count + 1
            when (@prevNum := Num) is not null then @count := 1
        end n
    from Logs, (select @prevNum := NULL) r
    order by Id
) a where n &gt;= 3


Two questions:

First when (@prevNum := Num) is not null what is the logic value of assignment expression in SQL?
I have googled MySQL and Oracle reference book, this forum and I got nothing about it. And this condition is really confusing.

Second the n after case end statement, I know n stands for the alias of case statement, but there are two variables in case end statement here, how do SQL know that n stands for @count but not @prevNum?

Thanks

The question is as follows:

Write a SQL query to find all numbers that appear at least three times consecutively.

+----+-----+
| Id | Num |
+----+-----+
| 1  |  1  |
| 2  |  1  |
| 3  |  1  |
| 4  |  2  |
| 5  |  1  |
| 6  |  2  |
| 7  |  2  |
+----+-----+


For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.
PROPOSED SOLUTION

Using the IF() function to check for consecutive num values

SET @x = 0;
SET @n = 0;
SELECT num,consecutive FROM
(SELECT
    id,num,
    (@x:=IF(num=@n,@x+1,1)) consecutive,
    (@n:=num) inc
FROM logs) A
WHERE consecutive &gt;= 3;


YOUR SAMPLE DATA

DROP DATABASE IF EXISTS tonychen;
CREATE DATABASE tonychen;
USE tonychen
CREATE TABLE logs
(
  id INT NOT NULL AUTO_INCREMENT,
  num INT NOT NULL,
  PRIMARY KEY (id)
);
INSERT INTO logs (num) VALUES (1),(1),(1),(2),(1),(2),(2);
SELECT * FROM logs;


YOUR SAMPLE DATA LOADED

mysql&gt; DROP DATABASE IF EXISTS tonychen;
Query OK, 1 row affected (0.25 sec)

mysql&gt; CREATE DATABASE tonychen;
Query OK, 1 row affected (0.00 sec)

mysql&gt; USE tonychen
Database changed
mysql&gt; CREATE TABLE logs
    -&gt; (
    -&gt;   id INT NOT NULL AUTO_INCREMENT,
    -&gt;   num INT NOT NULL,
    -&gt;   PRIMARY KEY (id)
    -&gt; );
Query OK, 0 rows affected (0.31 sec)

mysql&gt; INSERT INTO logs (num) VALUES (1),(1),(1),(2),(1),(2),(2);
Query OK, 7 rows affected (0.05 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql&gt; SELECT * FROM logs;
+----+-----+
| id | num |
+----+-----+
|  1 |   1 |
|  2 |   1 |
|  3 |   1 |
|  4 |   2 |
|  5 |   1 |
|  6 |   2 |
|  7 |   2 |
+----+-----+
7 rows in set (0.00 sec)

mysql&gt;


PROPOSED SOLUTION EXECUTED

mysql&gt; SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql&gt; SET @n = 0;
Query OK, 0 rows affected (0.00 sec)

mysql&gt; SELECT num,consecutive FROM
    -&gt; (SELECT
    -&gt;     id,num,
    -&gt;     (@x:=IF(num=@n,@x+1,1)) consecutive,
    -&gt;     (@n:=num) inc
    -&gt; FROM logs) A
    -&gt; WHERE consecutive &gt;= 3;
+-----+-------------+
| num | consecutive |
+-----+-------------+
|   1 |           3 |
+-----+-------------+
1 row in set (0.00 sec)

mysql&gt;


GIVE IT A TRY !!!