:=运算符在mysql中是什么意思? [英] What does the := operator mean in mysql?

查看:589
本文介绍了:=运算符在mysql中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mysql表(scho_idschool_nameschool_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆