Difference between = and := in MySQL

2019-07-24 19:23发布

问题:

in MySQL what is the difference between these two command?

They work perfectly and the result is always the same:

set @numRecords = (select count(*) from config);

set @numRecords := (select count(*) from config);

Thanks Davide

回答1:

Quoting the MySQL 5.7 Reference Manual, section 10.4 User-Defined Variables:

For SET, either = or := can be used as the assignment operator.

You can also assign a value to a user variable in statements other than SET. In this case, the assignment operator must be := and not = because the latter is treated as the comparison operator = in non-SET statements



回答2:

"=" is ambiguous and could be a comparison operator. ":=" is always interpreted as an assignment operator. This information can be found at http://dev.mysql.com/doc/refman/5.7/en/assignment-operators.html .