MySQL中“:=”和“=”的区别浅析

=

只有在set和update时才是和:=一样,赋值的作用,其它都是等于的作用。鉴于此,用变量实现行号时,必须用:=。

:=

不只在set和update时时赋值的作用,在select也是赋值的作用。

  下面是:=和=的在select时作用对比

sql

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

结果

MySQL中“:=”和“=”的区别浅析

sql

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

结果

MySQL中“:=”和“=”的区别浅析

sql

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

结果

MySQL中“:=”和“=”的区别浅析

总结

相关推荐