:=运算符在mysql中是什么意思? [英] What does the := operator mean in mysql?
问题描述
我有一个mysql
表(scho_id
,school_name
,school_views
).
I have a mysql
table (scho_id
,school_name
,school_views
).
我正在寻找一个mysql
查询,以基于school_views
来获得rank
学校.
I was looking for a mysql
query to get rank
of schools on the basis of school_views
.
我在stackoverflow上找到了这个解决方案.
I found this solution on stackoverflow.
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.
推荐答案
在MySQL中,:=
是赋值运算符:
In MySQL, :=
is an assignment operator:
SELECT @foo := 'bar'; // variable 'foo' now has value 'bar'
return value: 'bar'
而=
是相等性测试:
SELECT @foo = 'hi mom'; // does variable 'foo' have the value 'hi mom';
return value: false ('bar' == 'hi mom' -> false)
请注意,您可以使用set
查询进行相等性测试和赋值:
Note that you CAN do both equality testing AND assignment with set
queries:
SET @foo = 'bar' = 'baz';
,这将导致@foo
被分配为false
,即'bar' = 'baz'
的布尔结果.它执行如下:
which will cause @foo
to be assigned false
, the boolean result of 'bar' = 'baz'
. It executes as the following:
SET @foo = ('bar' = 'baz');
SET @foo = false;
这篇关于:=运算符在mysql中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!