MySQL > select * from ‘:=’

=

  • This is true only when set and update is set. For this reason, when implementing line numbers with variables, you must use :=.

: =

  • Assignment applies not only to sets and updates, but also to select.

Select * from (select * from (select *))

sql

set @num = 0;
SELECT @num := @num+1 AS rowno, nc as nickname from table_user;
Copy the code

The results of

sql

set @num = 0;
SELECT @num = @num+1 AS rowno, nc as nickname from table_user;
Copy the code

The results of

*@num is not equal to @num + 1, so rowno has the value 0*

sql

set @num = 0;
SELECT @num = @num AS rowno, nc as nickname from table_user;
Copy the code

The results of

*@num is equal to @num, so rowno has the value 1*

References:

  • Blog.csdn.net/wangjun5159…