简体   繁体   English

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

[英]What does the := operator mean in mysql?

I have a mysql table ( scho_id , school_name , school_views ). 我有一个mysql表( scho_idschool_nameschool_views )。

I was looking for a mysql query to get rank of schools on the basis of school_views . 我正在寻找一个mysql查询来根据school_views获得学校rank

I found this solution on stackoverflow. 我在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. 我很想知道这个运算符的含义和用法。

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' . 这将导致@foo被赋值为false ,布尔结果为'bar' = 'baz' It executes as the following: 它执行如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM