在mysql中:=运算符是什么意思?

时间:2021-09-13 22:28:46

I have a mysql table (scho_id,school_name,school_views).

我有一个mysql表(scho_id,school_name,school_views)。

I was looking for a mysql query to get rank of schools on the basis of school_views.

我正在寻找一个mysql查询来根据school_views获得学校排名。

I found this solution on *.

我在*上找到了这个解决方案。

SET @points := -1, @num := 0;
SELECT scho_id
, school_views
, @num := if(@points = school_views, @num, @num + 1) as school_rank
, @points := school_info.school_views as dummy
FROM school_info
ORDER BY school_views desc, scho_id asc;

This solved my problem but I notice a new operator := in this query. I am curious to know the meaning and uses of this operator.

这解决了我的问题,但我注意到一个新的运算符:=在此查询中。我很想知道这个运算符的含义和用法。

1 个解决方案

#1


17  

In MySQL, := is an assignment operator:

在MySQL中,:=是赋值运算符:

SELECT @foo := 'bar';    // variable 'foo' now has value 'bar'
return value: 'bar'

while = is an equality test:

while =是一个相等测试:

SELECT @foo = 'hi mom'; // does variable 'foo' have the value 'hi mom';
return value: false   ('bar' == 'hi mom' -> false)

Note that you CAN do both equality testing AND assignment with set queries:

请注意,您可以使用set查询执行相等性测试和赋值:

SET @foo = 'bar' = 'baz';

which will cause @foo to be assigned false, the boolean result of 'bar' = 'baz'. It executes as the following:

这将导致@foo被赋值为false,布尔结果为'bar'='baz'。它执行如下:

SET @foo = ('bar' = 'baz');
SET @foo = false;

#1


17  

In MySQL, := is an assignment operator:

在MySQL中,:=是赋值运算符:

SELECT @foo := 'bar';    // variable 'foo' now has value 'bar'
return value: 'bar'

while = is an equality test:

while =是一个相等测试:

SELECT @foo = 'hi mom'; // does variable 'foo' have the value 'hi mom';
return value: false   ('bar' == 'hi mom' -> false)

Note that you CAN do both equality testing AND assignment with set queries:

请注意,您可以使用set查询执行相等性测试和赋值:

SET @foo = 'bar' = 'baz';

which will cause @foo to be assigned false, the boolean result of 'bar' = 'baz'. It executes as the following:

这将导致@foo被赋值为false,布尔结果为'bar'='baz'。它执行如下:

SET @foo = ('bar' = 'baz');
SET @foo = false;