mysql 变量赋值 [英] Variable assignment with mysql

查看:43
本文介绍了mysql 变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mysql 中使用变量赋值,我发现了一个奇怪的行为.

I use variable assignment with mysql and I found a strange behavior.

查看此查询:

SET @v1=0;
SET @v2=0;

SELECT @v1, @v2 
FROM MyTable table 
WHERE (@v1:=@v2) is not null 
  AND (@v2:=2) is not null;

我认为条件是按以下顺序解析的:第一个 (@v1:=@v2) 不为空并且在 (@v2:=2) 之后不为空

I was thinking that conditions are parsed in this order : first (@v1:=@v2) is not null and after (@v2:=2) is not null

所以结果必须是:

@v1 | @v2
---------
 0  |  2

但事实并非如此.试试这个查询,你会得到:

But this is not the case. try this query and you will have:

@v1 | @v2
---------
 2  |  2

为什么?

推荐答案

SQL 是一种声明性语言,因此您不能假设您的条件按照您编写它们的顺序进行评估.你告诉引擎你想要什么,它可以自由决定如何得到它.

SQL is a declarative language, so you cannot assume that your conditions are evaluated in the order you write them. You tell the engine what you want and it is free to determine how to get it.

这篇关于mysql 变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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