Mysql adds serial number to query result

 set @rownum=0;      
 select  @rownum:=@rownum+1 as rownum,* from table1
What is the difference between := and =?
  1. "=", only in set and update, it is the same as :=, the role of assignment, and the others are equal. For this reason, when implementing line numbers with variables, you must use :=
  2. ":=", not only the role of assignment in set and update, but also the role of assignment in select.

If you understand the difference between = and :=, then you will understand the following phenomenon. 

@num:=@num+1,:= is the function of assignment, so execute @num+1 first, and then assign it to @num, so the function of line number can be correctly realized.

@num=@num+1, at this time = is equal, @num is not equal to @num+1, so it always returns 0, if it is changed to @num=@num, it always returns 1. In the mysql database, use 1 for true and 0 for false. 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324844251&siteId=291194637